C语言 C 指向结构的双指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7638612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:48:05  来源:igfitidea点击:

C Double Pointer to Structure

cpointersdata-structuresstruct

提问by linsek

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:

我正在尝试在 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);
}

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?

这里提出了一个类似的问题:How to work with pointer to pointer to pointer to structure in C? 关于如何通过双指针修改结构的成员。解决方案是(*data)->member = 1;有意义的语法。但是在我这里的小应用程序中,我在执行该行时收到一个段错误。我究竟做错了什么?

Thanks

谢谢

采纳答案by DwB

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);
}

回答by Ray Toal

You received a segfault because you did not allocate a struct.

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

The value of datais garbage, so it is pointing to some place in memory that is not owned by your process, or is otherwise inaccessible.

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

You need to first allocate an object of type mystruct. Here is a working example for you: http://ideone.com/XIdJ8

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

回答by gspr

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

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

  1. Create a mystruct.
  2. Make a pointer to it.
  3. Make a pointer to that pointer.
  1. 创建一个mystruct.
  2. 做一个指向它的指针。
  3. 制作一个指向该指针的指针。

回答by luser droog

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:

我从来没有使用过 libusb,所以我将举一个使用标准库函数的例子。从联机帮助页:

   #include <stdlib.h>

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

It only lookslike 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. strtolreturns a long integer but it also can tell you at what point the string contents stopped looking like a number.

它只是看起来像一个双指针。它实际上是一个模拟的通过引用传递的单指针。允许函数返回正常返回值之外的额外信息。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;
}

Output:

输出:

Quantity: 99;    Description: RED BALLOONS;

回答by Chris Eberle

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);
}