home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!vuse.vanderbilt.edu!leegye
- From: leegye@vuse.vanderbilt.edu (Gyesung Lee)
- Subject: structure question
- Message-ID: <1992Jul21.053858.26677@vuse.vanderbilt.edu>
- Originator: leegye@unleaded
- Sender: news@vuse.vanderbilt.edu (News Manager)
- Nntp-Posting-Host: unleaded
- Organization: Vanderbilt University School of Engineering, Nashville, TN, USA
- Distribution: usa
- Date: Tue, 21 Jul 1992 05:38:58 GMT
- Lines: 73
-
- The following is a simple program, where a structure is defined with 3 elements
- and 'main' calls 'abc' and it calls 'abc1'.
-
- /*----------------------------------*/
- #include <stdio.h>
-
- typedef struct a_struct {
- char *name;
- int mark;
- struct a_struct *next;
- } *A_Type;
-
- A_Type Global_Root;
-
-
- abc1()
- {
- if (Global_Root->next == NULL)
- printf("In abc1: is NULL\n");
- else
- printf("In abc1: not NULL\n");
- }
-
- abc() /* check next ptr of Global_Root */
- {
- if (Global_Root->next == NULL)
- printf("In abc: is NULL\n");
- else
- printf("In abc: not NULL\n");
- abc1();
- }
-
-
- main () /* create a global root and set next ptr to NULL */
- {
- Global_Root = (A_Type) malloc (sizeof(A_Type));
- Global_Root->next = NULL;
- abc();
- }
-
- /*----------------------------------*/
-
- The right answer should be:
-
- In abc: is NULL
- In abc1: is NULL
-
- But, the result turns out as follows:
-
- In abc: is NULL
- In abc1: not NULL
- Segmentation fault (core dumped)
-
- If I modify 'A_Type' structure as follows by switching `mark' and `next',
-
- typedef struct a_struct {
- char *name;
- struct a_struct *next;
- int mark;
- } *A_Type;
-
- then, it works correctly.
-
- I rearranged the elements of `struct a_struct' and ran it.
- In some case it works, in other cases no luck.
- Could anyone tell me what is wrong with this program ?
-
- Thank you for your help.
-
- Gyesung Lee.
- leegye@vuse.vanderbilt.edu
-
-
-