Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

nested structures in C

Posted on 2005-05-03
5
Medium Priority
?
12,607 Views
Last Modified: 2010-05-18
I having a  nested  structures which are referenced with pointer variables

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


While trying to access the value of i as mysample->test1->i it throws segmentation fault error.
  How to access this variable.


Help me

Regards,
Priyashree
0
Comment
Question by:priyashree
2 Comments
 
LVL 21

Accepted Solution

by:
MogalManic earned 200 total points
ID: 13925092
When you declare a pointer, you are telling the compiler, that this variable will point to memory of this description.  For ALL pointers, you must do the allocations your self.  To correct your sample code, insert the following code:
  struct sample *mysample = malloc(sizeof(struct sample));
  if (mysample==NULL) {
     printf("Out of memory error")
     return -1;
  }
 //At this point you have ONLY allocated sample, NOT the contents of sample
 sample->test1=malloc(sizeof(struct test));
 if (sample->test1=NULL) {
     printf("Out of memory error")
     return -1;
 }
0
 
LVL 4

Expert Comment

by:furqanchandio
ID: 14051852
hi
try this


struct test *test1= (struct test *) malloc (sizeof(test));
struct sample *mysample= (struct sample *) malloc (sizeof(sample));
  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


as magal has said
you declared only a pointer to structure sample and test without reserving any space

cheers
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question