字符串到 char (*array)[]
string to char (*array)[]
//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: error: array size is negative
我编写了一个 webhook 函数,但触发了 notifyWebhook 函数并发送了错误的参数。
为什么?我在哪里犯错?
string to char (*array)[]
cgo-gcc-prolog: error: array size is negative
使用垫片。例如,
so.go
:
// string to char (*array)[]
package main
/*
#include <stdlib.h>
#include <stdio.h>
void notify(char (*message)[]) {
printf("notify | argv: %1$p %1$s\n", *message);
}
void shimmy(char *message[]) {
printf("shimmy | argv: %1$p %1$s\n", *message);
notify((char (*)[])*message);
}
*/
import "C"
import (
"fmt"
"unsafe"
)
func main() {
message := "My Message :)"
cs := C.CString(message)
defer C.free(unsafe.Pointer(cs))
fmt.Printf("main | argv: %p %s\n", cs, C.GoString(cs))
C.shimmy(&cs)
}
输出:
$ go run so.go
main | argv: 0x19cb820 My Message :)
shimmy | argv: 0x19cb820 My Message :)
notify | argv: 0x19cb820 My Message :)
$
$ go version
go version devel +4d5bb9c609 Fri Dec 20 23:07:52 2019 +0000 linux/amd64
$ go env CC
gcc
$ gcc --version
gcc (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008
$
//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: error: array size is negative
我编写了一个 webhook 函数,但触发了 notifyWebhook 函数并发送了错误的参数。 为什么?我在哪里犯错?
string to char (*array)[]
cgo-gcc-prolog: error: array size is negative
使用垫片。例如,
so.go
:
// string to char (*array)[]
package main
/*
#include <stdlib.h>
#include <stdio.h>
void notify(char (*message)[]) {
printf("notify | argv: %1$p %1$s\n", *message);
}
void shimmy(char *message[]) {
printf("shimmy | argv: %1$p %1$s\n", *message);
notify((char (*)[])*message);
}
*/
import "C"
import (
"fmt"
"unsafe"
)
func main() {
message := "My Message :)"
cs := C.CString(message)
defer C.free(unsafe.Pointer(cs))
fmt.Printf("main | argv: %p %s\n", cs, C.GoString(cs))
C.shimmy(&cs)
}
输出:
$ go run so.go
main | argv: 0x19cb820 My Message :)
shimmy | argv: 0x19cb820 My Message :)
notify | argv: 0x19cb820 My Message :)
$
$ go version
go version devel +4d5bb9c609 Fri Dec 20 23:07:52 2019 +0000 linux/amd64
$ go env CC
gcc
$ gcc --version
gcc (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008
$