C 指向结构的双指针 [英] C Double Pointer to Structure

查看:19
本文介绍了C 指向结构的双指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 C 中计算出一个指向结构的双指针,但无法弄清楚出了什么问题......简单的来源如下:

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
", (*data)->member);
}

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

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?

谢谢

推荐答案

如果您要取消引用一个指针,您需要指向某物.试试这个:

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

这篇关于C 指向结构的双指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆