C 结构体双指针

发布于 2024-12-08 00:13:46 字数 576 浏览 1 评论 0原文

我正在尝试计算出一个指向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

黑白记忆 2024-12-15 00:13:47

如果要取消引用指针,则需要指向某个东西。试试这个:

void main(int argc, char *argv)
{
    mystruct actualThing;
    mystruct *pointer = &actualThing;
    mystruct **data = &pointer;
    myfunc(data);

    printf("Member: %d", (*data)->member);
}

You need to point to something if you are going to dereference a pointer. Try this:

void main(int argc, char *argv)
{
    mystruct actualThing;
    mystruct *pointer = &actualThing;
    mystruct **data = &pointer;
    myfunc(data);

    printf("Member: %d", (*data)->member);
}
溺渁∝ 2024-12-15 00:13:47

您收到了段错误,因为您没有分配结构。

data 的值是垃圾,因此它指向内存中不属于您的进程的某个位置,或者无法访问。

您需要首先分配一个 mystruct 类型的对象。这是一个适合您的工作示例:http://ideone.com/XIdJ8

You 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/XIdJ8

冷月断魂刀 2024-12-15 00:13:47

data 未初始化,因此不指向任何合理的内存地址。此外,没有 mystruct 结构浮动,因此实际上甚至没有任何可指向的有意义的数据。对于您的示例,您想要:

  1. 创建一个 mystruct
  2. 做一个指向它的指针。
  3. 建立一个指向该指针的指针。

data is not initialized, and hence doesn't point to any sensible memory address. Moreover, there is no mystruct structure floating around, so there really isn't even any sensible data to point to. For your example, you want to:

  1. Create a mystruct.
  2. Make a pointer to it.
  3. Make a pointer to that pointer.
没有伤那来痛 2024-12-15 00:13:47

如果您只需将双指针传递给库函数,则不需要为其创建变量。您创建一个普通的指针变量,将其初始化为指向适当的存储(如果函数需要),然后传递指针的地址(从而“动态”创建双指针)。

我从未使用过 libusb,因此我将给出一个使用标准库函数的示例。从联机帮助页来看:

   #include <stdlib.h>

   long int strtol(const char *nptr, char **endptr, int base);

它只是看起来像一个双指针。它实际上是一个模拟的按引用传递的单指针。允许函数返回除正常返回值之外的额外信息。 strtol 返回一个长整数,但它也可以告诉您字符串内容在什么时候不再看起来像数字。

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char *str = "99RED BALLOONS";
    char *what;
    long num;

    num = strtol(str, &what, 10);
    printf("Quantity: %ld;    Description: %s;\n", num, what);

    return 0;
}

输出:

Quantity: 99;    Description: RED BALLOONS;

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:

   #include <stdlib.h>

   long int strtol(const char *nptr, char **endptr, int base);

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.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char *str = "99RED BALLOONS";
    char *what;
    long num;

    num = strtol(str, &what, 10);
    printf("Quantity: %ld;    Description: %s;\n", num, what);

    return 0;
}

Output:

Quantity: 99;    Description: RED BALLOONS;
怀中猫帐中妖 2024-12-15 00:13:47

或者也许你可以尝试这个:

void main(int argc, char*argv[])
{
   mystruct *data;
   myfunc(&data);
   printf("member = %d\n", data->member);
}

这对我来说在 C++ 中有效,不需要指向另一个变量。

Or maybe you can try this:

void main(int argc, char*argv[])
{
   mystruct *data;
   myfunc(&data);
   printf("member = %d\n", data->member);
}

This works for me in C++ and not needed to point another variable.

人心善变 2024-12-15 00:13:47

您向它传递了一个指针,但该指针没有指向任何东西。

这可能更有用:

void main(int argc, char *argv[])
{
    mystruct data;
    mystruct *ptr = &data;
    myfunc(&ptr);
    printf("member = %d\n", (*ptr)->member);
}

You're passing it a pointer, but the pointer isn't pointing at anything.

This may be more useful:

void main(int argc, char *argv[])
{
    mystruct data;
    mystruct *ptr = &data;
    myfunc(&ptr);
    printf("member = %d\n", (*ptr)->member);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文