home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!seas.gwu.edu!saud
- From: saud@seas.gwu.edu (Temporary account (slc 11/19/92))
- Subject: pointer assignment problem
- Message-ID: <1992Dec13.181118.2693@seas.gwu.edu>
- Sender: news@seas.gwu.edu
- Organization: George Washington University
- Distribution: usa
- Date: Sun, 13 Dec 1992 18:11:18 GMT
- Lines: 104
-
- Hi:
-
- I am having problem with pointer assignment
-
- look at this pleae
-
- function_node *tmp_node;
-
- tmp_node = (function_node *) malloc (sizeof(tmp_node));
- ^^^^^^^
- If I examine the content of tmp_node (p *tmp_node) in the debugger
- I got
- func_name = null ; pointer to char
- def_or_dec = null ; pointer to char
- line_number = 0 ; Int
- basic_type = ""; /* I do not know why even though its a point to char
- class_enclose = 0; /* pointer to an other structure
- next =0; /* pointer to function_node structure (this struc)
-
- ^^^^^^^^^
- tmp_node->func_name = save_name (func_name);
- save_name (code is eclosed) simple it will allocate space the copy this string
- ^^^^^^^^^
- Now is the fun part
- once the above line gets executed the pointer class_enclose and next
- get some garbage (values that does not make sense). any way wait
-
- /* link to the CCB struct that encloses this function */
- if ( class_count ) {
- tmp_node->class_enclose = class_ptr;
- OR with castign as
- /* tmp_node->class_enclose = (class_node *)class_ptr; */
- }
- ^^^^^^^^^^^
- here is the major problem
- once the above line gets execute:...... The value of the pointer func_name gets
- garbage suech as
- "@\002\213\X"
- for example if you print the *tmp_node before executing the pointer assignment
- I got
- tmp_node->func_name = "Basic_const";
- after the pointer assignment I got
- tmp_node->fun_name ="@\002\213\X_const"; part of the name was overwritten
-
- could some one help please
-
- thanks
- wanes
-
- here is th complete code if that help
-
- THE SYSTEM THIS IS RUN ON IS HP-BOX 835E I guess
- ---------------------------
- add_func(s)
- char * s;
- {
-
- function_node *tmp_node;
-
- tmp_node = (function_node *) malloc (sizeof(tmp_node));
-
- tmp_node->func_name = save_name (func_name);
- if ( basic_type_name != NULL) {
- tmp_node->basic_type = basic_type_name;
- basic_type_name = NULL;
- }
- tmp_node->is_it_friend = is_it_friend;
- if (current_access_mode != NULL){
- tmp_node->access_type = save_name(current_access_mode);
- }
- /* link to the CCB struct that encloses this function */
- if ( class_count ) {
- /* tmp_node->class_enclose = malloc(sizeof(class_node)); */
- tmp_node->class_enclose = (class_node *)class_ptr;
- }
- tmp_node->line_number = yylineno;
- tmp_node->def_or_dec = save_name(s);
- /*
- Link this functin to the list of the functions
- */
-
- if ( func_head == NillFunc) {
- func_head = tmp_node;
- func_tail = tmp_node;
- func_head->next = NillFunc;
- }
- else {
- func_tail->next = tmp_node;
- func_tail = tmp_node ;
- }
- }
-
-
- char *save_name(item_in)char *item_in;
- {
- register char *dp;
- int len ;
-
- len = strlen(item_in);
- dp = (char *)malloc(len+1);
- strncpy(dp,item_in,len);
- dp[len] ='\0';
- return dp;
- }
-