problem with nested structures

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samba
    New Member
    • Mar 2007
    • 3

    problem with nested structures

    I having a nested structures which are referenced with pointer variables
    Code:
    #include <stdio.h>
    struct test
    {
      int i;
      char *mytest;
    };
    struct sample
    {
      int k;
      struct test *test1;
    };
    int main()
    {
      struct sample *mysample;
      printf("Inside main method \n\n");
      mysample->k=0;
      mysample->test1->i=1;  // It's throws segmentation fault error at this point how to access this variable.
      printf("the value of the k is  %d \n\n",mysample->k);
      //  printf("the value of the k is  %d \n\n",mysample->test->i);
      return 0;
    }
    please help me as soon as possible
    Last edited by horace1; Mar 19 '07, 01:25 PM. Reason: added code tags
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Moved to C++ forum.

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      you need to point the pointer sat some objects, e.g.
      Code:
       struct sample s;
        struct sample *mysample=&s;
        printf("Inside main method \n\n");
        mysample->k=0;
        struct test t;
        mysample->test1=&t;  
        mysample->test1->i=1;  // It's throws segmentation fault error at this point how to access this variable.

      Comment

      Working...