C 结构体双指针
我正在尝试计算出一个指向 C 结构的双指针,但无法弄清楚出了什么问题...简单的来源如下:
typedef struct
{
int member;
} mystruct;
void myfunc(mystruct **data)
{
(*data)->member = 1;
}
void main(int argc, char *argv[])
{
mystruct **data;
myfunc(data);
printf("member = %d\n", (*data)->member);
}
这里提出了类似的问题: 如何在 C 中使用指向结构体的指针? 关于如何修改成员通过双指针的结构。解决方案是语法 (*data)->member = 1;
,这是有意义的。但在我的小应用程序中,执行该行时我收到一个段错误。我做错了什么?
谢谢
I am trying to work out a double pointer to a structure in C and cannot figure out what is going wrong... The simple source is below:
typedef struct
{
int member;
} mystruct;
void myfunc(mystruct **data)
{
(*data)->member = 1;
}
void main(int argc, char *argv[])
{
mystruct **data;
myfunc(data);
printf("member = %d\n", (*data)->member);
}
A similar question was asked here: How to work with pointer to pointer to structure in C? on how to modify a member of a structure through a double pointer. The solution was the syntax (*data)->member = 1;
which makes sense. But in my little application here, I receive a seg fault when executing that line. What am I doing wrong?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果要取消引用指针,则需要指向某个东西。试试这个:
You need to point to something if you are going to dereference a pointer. Try this:
您收到了段错误,因为您没有分配结构。
data
的值是垃圾,因此它指向内存中不属于您的进程的某个位置,或者无法访问。您需要首先分配一个
mystruct
类型的对象。这是一个适合您的工作示例:http://ideone.com/XIdJ8You received a segfault because you did not allocate a struct.
The value of
data
is garbage, so it is pointing to some place in memory that is not owned by your process, or is otherwise inaccessible.You need to first allocate an object of type
mystruct
. Here is a working example for you: http://ideone.com/XIdJ8data
未初始化,因此不指向任何合理的内存地址。此外,没有mystruct
结构浮动,因此实际上甚至没有任何可指向的有意义的数据。对于您的示例,您想要:mystruct
。data
is not initialized, and hence doesn't point to any sensible memory address. Moreover, there is nomystruct
structure floating around, so there really isn't even any sensible data to point to. For your example, you want to:mystruct
.如果您只需将双指针传递给库函数,则不需要为其创建变量。您创建一个普通的指针变量,将其初始化为指向适当的存储(如果函数需要),然后传递指针的地址(从而“动态”创建双指针)。
我从未使用过 libusb,因此我将给出一个使用标准库函数的示例。从联机帮助页来看:
它只是看起来像一个双指针。它实际上是一个模拟的按引用传递的单指针。允许函数返回除正常返回值之外的额外信息。
strtol
返回一个长整数,但它也可以告诉您字符串内容在什么时候不再看起来像数字。输出:
If you only need to pass the double pointer to a library function, you don't need to create a variable for it. You make a normal pointer variable, initialize it to point to appropriate storage (if required by the function), then pass the address of the pointer (thus creating the double-pointer "on the fly").
I've never used libusb, so I'll give an example using a standard library function. From the manpage:
It only looks like a double-pointer. It's really a simulated-pass-by-reference single pointer. Allowing the function to return extra information besides its normal return value.
strtol
returns a long integer but it also can tell you at what point the string contents stopped looking like a number.Output:
或者也许你可以尝试这个:
这对我来说在 C++ 中有效,不需要指向另一个变量。
Or maybe you can try this:
This works for me in C++ and not needed to point another variable.
您向它传递了一个指针,但该指针没有指向任何东西。
这可能更有用:
You're passing it a pointer, but the pointer isn't pointing at anything.
This may be more useful: