我是 C 语言新手。我的代码是这样的:
int afunc(const struct datas *mydata, void *value) {
value = &mydata->astring; // astring is in structure char[20]
return 0;
}
int main (...) {
...
const char *thevalue;
if (!afunc(thedata, &thevalue) {...}
...
}
var 值中的地址仅在函数中,当函数结束变量时,该值仍然为空...所以,我想要结构中数组上的指针。
我应该如何解决这个问题?
最佳答案
像这样修复
#include <stdio.h>
struct datas {
char astring[20];
};
int afunc(const struct datas *mydata, void *value) {
*(const char **)value = mydata->astring;
return 0;
}
int main (void) {
struct datas mydata = { "test_data" }, *thedata = &mydata;
const char *thevalue;
if (!afunc(thedata, &thevalue)) {
puts(thevalue);
}
}
关于c - void * 函数的输入在函数结束时丢失地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43577632/