【问题标题】:Pointer To A Nested Structure指向嵌套结构的指针
【发布时间】:2013-04-10 10:07:55
【问题描述】:

这是我的代码...

#include <stdio.h>

struct one
{
    struct two
    {
            int r;
    }*b;
}*a;

void main()
{
    //struct two *new = &(*a).b;
    //new->r = 10;
    //printf("Value: %d", new->r);
    a = malloc(sizeof(struct one));
    //b = malloc(sizeof(struct two));
    (a->b)->r = 10;
    printf("Value: %d", (a->b)->r);
    return 0;

}

我在这里尝试的是,将结构定义为结构。现在两个对象都应该是指针。我想设置r的值,然后显示出来。

我唯一得到它Segmentation Fault 使用gdb 我得到了关注,这似乎没有多大帮助..

(gdb) run
Starting program: /home/sujal.p/structtest/main

Program received signal SIGSEGV, Segmentation fault.
0x08048435 in main ()

我想知道如何执行上述操作以及为什么这件事会出现分段错误。我已经尝试了一些网站上可用的可能方法,包括 Stackoverflow 的一些问题。

注释行是我尝试实现目标但失败并出现相同错误的失败。

编辑在尝试了下面提到的技术之后..

void main()
{
    //struct two *new = &(*a).b;
    //new->r = 10;
    //printf("Value: %d", new->r);

    //a = malloc(sizeof(struct one));
    //a my_a = malloc(sizeof*my_a);
    //my_a->b = malloc(sizeof *my_a->b);
    //my_a->b->r = 10;
    //b = malloc(sizeof(struct two));
    //(a->b)->r = 10;
    //printf("Value: %d", my_a->b->r);

    a = (one*)malloc(sizeof(struct one));
    a->b = (one::two*)malloc(sizeof(struct one::two));
    (a->b)->r = 10;
    printf("Value: %d", (a->b)->r);
    return 0;

}

我已经尝试了所有提到的技术,但他们给了我错误..我得到的最后一个错误如下..

new.c: In function âmainâ:
new.c:24:7: error: âoneâ undeclared (first use in this function)
new.c:24:7: note: each undeclared identifier is reported only once for each function it     appears in
new.c:24:11: error: expected expression before â)â token
new.c:25:13: error: expected â)â before â:â token
new.c:25:20: error: expected â;â before âmallocâ
new.c:28:2: warning: âreturnâ with a value, in function returning void [enabled by default]

【问题讨论】:

    标签: c pointers gcc struct


    【解决方案1】:

    您正在取消引用一个未初始化的指针。

    你需要先分配一个struct one的实例:

    a = malloc(sizeof *a);
    

    那么就可以初始化成员b

    a->b = malloc(sizeof *a->b);
    

    然后你就可以访问r:

    a->b->r = 10;
    

    Here is a working solution, by adapting your code with my answer.

    【讨论】:

    • 这是我得到的一个错误...new.c: In function âmainâ: new.c:17:4: error: expected â;â before âmy_aâ new.c:18:2: error: âmy_aâ undeclared (first use in this function) new.c:18:2: note: each undeclared identifier is reported only once for each function it appears in new.c:18:12: warning: incompatible implicit declaration of built-in function âmallocâ [enabled by default] new.c:22:2: error: expected â;â before âprintfâ new.c:23:2: warning: âreturnâ with a value, in function returning void [enabled by default]
    • 不适合我...这让我发疯...它正在冲淡我的概念...
    • 呃,不知道你为什么在你的代码中使用一堆::,那是 C++ 而不是有效的 C 语法。还有there should be no casts of malloc()'s return value, in C.
    • 这只是为了看看这里发布的答案是否会派上用场......下面的答案是建议我这样做的答案......
    【解决方案2】:

    你得到一个 SIGSEGV,因为第二个解引用的指针

    (a->b)->r
    

    未初始化/未定义

    要解决你需要做的问题

    struct two
    {
            int r;
    }
    
    struct one
    {
     two *b;
    };
    
    one *a;
    
    ...
    
    a = malloc(sizeof(struct one));
    a->b = malloc(sizeof(struct two));
    
    a->b->r = 10;
    printf("Value: %d", a->b->r);
    return 0;
    

    【讨论】:

    • 是的,完全正确...但是我尝试过(*a).(*b).ra-&gt;b-&gt;r,但都失败了...
    • 是的,我可以做到。但它限制了我使用上述结构...
    【解决方案3】:

    除了@Quonux 的回答,我还要定义类型:

    typedef struct
    {
        int r;
    } t_two;
    
    typedef struct
    {
        t_two *p_b;
    } t_one;
    
    void main()
    {
        t_one *p_a;
    
        p_a = malloc(sizeof(t_one);
        p_a->p_b = malloc(sizeof(t_two));
    
        p-a->p_b->r = 10;
    
        printf("Value: %d", p_a->p_b->r);
        return 0;
    
    }
    

    【讨论】:

    • 感谢您的努力.. 但问题是.. 使用上述结构方式对我来说是一个限制.. 我的意思是我只能使用 struct one { struct two { int r; }; };..
    【解决方案4】:
    a = malloc(sizeof(*a));
    a->b = malloc(sizeof(*a->b));
    a->b->r = 10;
    printf("Value: %d", a->b->r);
    free(a->b);
    free(a);
    return 0;
    

    【讨论】:

    • 谢谢.. 但是遇到这么多错误...new.c: In function âmainâ: new.c:24:7: error: âoneâ undeclared (first use in this function) new.c:24:7: note: each undeclared identifier is reported only once for each function it appears in new.c:24:11: error: expected expression before â)â token new.c:25:13: error: expected â)â before â:â token new.c:25:20: error: expected â;â before âmallocâ new.c:28:2: warning: âreturnâ with a value, in function returning void [enabled by default]
    猜你喜欢
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多