home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11385 < prev    next >
Encoding:
Text File  |  1992-07-20  |  1.9 KB  |  87 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!vuse.vanderbilt.edu!leegye
  3. From: leegye@vuse.vanderbilt.edu (Gyesung Lee)
  4. Subject: structure question
  5. Message-ID: <1992Jul21.053858.26677@vuse.vanderbilt.edu>
  6. Originator: leegye@unleaded
  7. Sender: news@vuse.vanderbilt.edu (News Manager)
  8. Nntp-Posting-Host: unleaded
  9. Organization: Vanderbilt University School of Engineering, Nashville, TN, USA
  10. Distribution: usa
  11. Date: Tue, 21 Jul 1992 05:38:58 GMT
  12. Lines: 73
  13.  
  14. The following is a simple program, where a structure is defined with 3 elements
  15. and 'main' calls 'abc' and it calls 'abc1'.
  16.  
  17. /*----------------------------------*/
  18. #include <stdio.h>
  19.  
  20. typedef struct a_struct {
  21.        char            *name;
  22.        int             mark;
  23.        struct a_struct *next;
  24. } *A_Type;
  25.  
  26. A_Type Global_Root;
  27.  
  28.  
  29. abc1()
  30. {
  31.    if (Global_Root->next == NULL)
  32.       printf("In abc1: is  NULL\n");
  33.    else
  34.       printf("In abc1: not NULL\n");
  35. }
  36.  
  37. abc()  /* check next ptr of Global_Root */
  38. {
  39.    if (Global_Root->next == NULL)
  40.       printf("In abc:  is  NULL\n");
  41.    else
  42.       printf("In abc:  not NULL\n");
  43.    abc1();
  44. }
  45.  
  46.  
  47. main  ()  /* create a global root and set next ptr to NULL */
  48. {
  49.   Global_Root = (A_Type) malloc (sizeof(A_Type));
  50.   Global_Root->next = NULL;
  51.   abc();
  52. }
  53.  
  54. /*----------------------------------*/
  55.  
  56. The right answer should be:
  57.  
  58. In abc:  is  NULL
  59. In abc1: is  NULL
  60.  
  61. But, the result turns out as follows:
  62.  
  63. In abc:  is  NULL
  64. In abc1: not NULL
  65. Segmentation fault (core dumped)
  66.  
  67. If I modify 'A_Type' structure as follows by switching `mark' and `next',
  68.  
  69. typedef struct a_struct {
  70.        char            *name;
  71.        struct a_struct *next;
  72.        int             mark;
  73. } *A_Type;
  74.  
  75. then, it works correctly.
  76.  
  77. I rearranged the elements of `struct a_struct' and ran it.
  78. In some case it works, in other cases no luck.
  79. Could anyone tell me what is wrong with this program ?
  80.  
  81. Thank you for your help.
  82.  
  83. Gyesung Lee.
  84. leegye@vuse.vanderbilt.edu
  85.  
  86.  
  87.