3

Here is my code...

#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;

}

What I am trying here is, defining a structure into a structure. Now both of the objects should be pointers. I want to set the value of r and then display it.

The only thing I am getting it Segmentation Fault using gdb I got following, which seems not helping much..

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

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

I want to know how to perform mentioned action and why this thing gets Segmentation fault. I have tried possible ways available on some of the websites including Stackoverflow's some of the questions.

The commented lines are my unsuccessful tried to achieve the goal but failed with the same error.

EDIT After trying below mentioned Techniques..

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;

}

I have tried all the mentioned techniques, and they are giving me errors.. The last error I get is as follows..

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]
Hiren Pandya
  • 989
  • 1
  • 7
  • 20

4 Answers4

9

You're de-referencing an uninitialized pointer.

You need to first allocate an instance of struct one:

a = malloc(sizeof *a);

then you can initialize the member b:

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

and then you can access r:

a->b->r = 10;

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

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Here is what I am getting as an error... `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]` – Hiren Pandya Apr 10 '13 at 10:13
  • Not working for me... This is driving me crazy... It is flushing my concepts away.. – Hiren Pandya Apr 10 '13 at 10:24
  • Uh, not sure why you're using a bunch of `::` in your code, that's C++ and not valid C syntax at all. Also [there should be no casts of `malloc()`'s return value, in C](http://stackoverflow.com/a/605858/28169). – unwind Apr 10 '13 at 10:29
  • Thats just to see whether the answers posted here are getting handy in cases or not... below answer is the one which suggested me to do so... – Hiren Pandya Apr 10 '13 at 10:31
0

you get an SIGSEGV because the pointer for the secound dereferencing

(a->b)->r

is not initialized/defined

to solve the problem you need to do following

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;
Quonux
  • 2,975
  • 1
  • 24
  • 32
0

Adding to @Quonux answer, I would as well define types:

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;

}
  • Thank you for your effort.. But the thing is.. Its a constraint for me to use the mentioned way of structure.. I mean I have to use `struct one { struct two { int r; }; };` only.. – Hiren Pandya Apr 10 '13 at 10:29
-1
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;
freddy.smith
  • 451
  • 4
  • 9
  • Thank you.. But got these many errors... `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]` – Hiren Pandya Apr 10 '13 at 10:16