我正在尝试创建一个链表,您可以在其中更新节点中的数据,但无论我尝试什么,C 似乎都不允许我更新 void 指针的值(或者更确切地说,它在哪里指着)。这是我的测试代码:
void newData(void * d)
{
char data[] = "world";
d = &data;
}
int main()
{
char testData[] = "hello";
void * testPointer = &testData;
printf("TestData is %s\n", (char *)testPointer);
// Modify the data
newData(&testPointer);
printf("TestData is %s\n", (char *)testPointer);
}
这只是输出::
TestData is hello
TestData is hello
我在这里遗漏了什么明显的东西吗?我也尝试过使用指向指针的指针,但无济于事。
最佳答案
我觉得你需要
void newData(void ** d)
{
char data[] = "world";
*d = &data;
}
但是,这有它自己的问题,因为“world”是本地堆栈,从 newData 返回后将无效。
关于c - 如何通过函数修改指向 void 的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4938006/