字符串为char(* array)[]

问题描述 投票:3回答:1
//file.go

func main() {

  message := "My Message :)"
  // I've tried this slice before.
  // tmpslice := (*[1 << 30]*C.char)(unsafe.Pointer(argv))[:length:length] 
  argv := make([]*C.char, len(message))
  for i, s := range str {
    cs := C.CString(string(s))
    defer C.free(unsafe.Pointer(cs))
    argv[i] = cs
  }

  C.notifyWebhook(&argv)

}
  //file.c
  void notiftWebhook(char (*message)[]) {
  printf("notiftWebhook executed | argv: %s \n", *message);
  char url[500];
  char data[200];


  int user_id = 0xdeadfeed;

  snprintf(url,500,"https://webhook.site/a6a8d1ae-6766-4d90-a4c8-87a9599bfbf0",token);
  snprintf(data,200,"user_id=%d&text=%s",user_id,*message);
  CURL *curl;
  //CURLcode res;

  //static const char *postthis = "moo mooo moo moo";

  curl_global_init(CURL_GLOBAL_ALL);
  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS,data);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_perform(curl);
  }
  curl_global_cleanup();
}

编译器返回:

cgo-gcc-prolog:129:19:错误:数组大小为负数

我编写了一个webhook函数,但触发了notifyWebhook函数并发送了错误的参数。为什么?我在哪里犯错?

go cgo gccgo
1个回答
2
投票

字符串为char(* array)[]

© www.soinside.com 2019 - 2024. All rights reserved.