home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18297 < prev    next >
Encoding:
Text File  |  1992-12-13  |  2.9 KB  |  116 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!seas.gwu.edu!saud
  3. From: saud@seas.gwu.edu (Temporary account (slc 11/19/92))
  4. Subject: pointer assignment problem
  5. Message-ID: <1992Dec13.181118.2693@seas.gwu.edu>
  6. Sender: news@seas.gwu.edu
  7. Organization: George Washington University
  8. Distribution: usa
  9. Date: Sun, 13 Dec 1992 18:11:18 GMT
  10. Lines: 104
  11.  
  12. Hi:
  13.  
  14. I am having problem with pointer assignment 
  15.  
  16. look at this pleae
  17.     
  18.     function_node    *tmp_node;
  19.  
  20.     tmp_node = (function_node *) malloc (sizeof(tmp_node));
  21. ^^^^^^^
  22. If I examine the content of tmp_node (p *tmp_node) in the debugger
  23. I got 
  24.     func_name = null ; pointer to char
  25.     def_or_dec = null ; pointer to char
  26.     line_number = 0 ; Int
  27.     basic_type = ""; /* I do not know why even though its a point to char
  28.     class_enclose = 0; /* pointer to an other structure
  29.     next =0;    /* pointer to function_node structure (this struc)
  30.  
  31. ^^^^^^^^^
  32.     tmp_node->func_name = save_name (func_name);
  33. save_name (code is eclosed) simple it will allocate space the copy this string
  34. ^^^^^^^^^
  35. Now is the fun part
  36.     once the above line gets executed the pointer class_enclose and next
  37. get some garbage (values that does not make sense). any way wait
  38.  
  39. /* link to the CCB struct that encloses this function */
  40.     if ( class_count ) {
  41.         tmp_node->class_enclose = class_ptr;
  42. OR with castign as
  43. /*        tmp_node->class_enclose = (class_node *)class_ptr;  */
  44.     }
  45. ^^^^^^^^^^^
  46. here is the major problem
  47. once the above line gets execute:...... The value of the pointer func_name gets
  48. garbage suech as
  49.     "@\002\213\X"
  50. for example if you print the *tmp_node before executing the pointer assignment
  51.     I got 
  52.     tmp_node->func_name = "Basic_const";
  53. after the pointer assignment I got 
  54.     tmp_node->fun_name ="@\002\213\X_const"; part of the name was overwritten
  55.  
  56. could some one help please
  57.  
  58. thanks
  59. wanes
  60.  
  61. here is th complete code if that help
  62.  
  63. THE SYSTEM THIS IS RUN ON IS HP-BOX 835E I guess
  64. ---------------------------
  65. add_func(s)
  66. char * s;
  67. {
  68.     
  69.     function_node    *tmp_node;
  70.  
  71.     tmp_node = (function_node *) malloc (sizeof(tmp_node));
  72.  
  73.     tmp_node->func_name = save_name (func_name);
  74.     if ( basic_type_name != NULL) {
  75.         tmp_node->basic_type = basic_type_name;
  76.          basic_type_name = NULL;
  77.     }
  78.     tmp_node->is_it_friend = is_it_friend;
  79.     if (current_access_mode != NULL){
  80.         tmp_node->access_type = save_name(current_access_mode);
  81.     }
  82. /* link to the CCB struct that encloses this function */
  83.     if ( class_count ) {
  84. /*        tmp_node->class_enclose = malloc(sizeof(class_node));  */
  85.         tmp_node->class_enclose = (class_node *)class_ptr;
  86.     }
  87.     tmp_node->line_number = yylineno;
  88.     tmp_node->def_or_dec    = save_name(s);
  89. /*
  90.     Link this functin to the list of the functions
  91. */
  92.  
  93.     if ( func_head == NillFunc) {
  94.         func_head = tmp_node;
  95.         func_tail = tmp_node;
  96.         func_head->next = NillFunc;
  97.     }
  98.     else {
  99.         func_tail->next = tmp_node;
  100.         func_tail = tmp_node ;
  101.     }
  102.  }        
  103.  
  104.  
  105. char *save_name(item_in)char *item_in;
  106. {
  107.     register char *dp;
  108.     int     len ;
  109.  
  110.     len = strlen(item_in);
  111.     dp = (char *)malloc(len+1);
  112.     strncpy(dp,item_in,len);
  113.     dp[len] ='\0';
  114.     return dp;
  115. }
  116.