Something wrong with pointers

This is a discussion on Something wrong with pointers within the C Programming forums, part of the General Programming Boards category; Does anybody have any idea why my output displays the 1d node name for both the 2d nodes "alice" and ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    20

    Something wrong with pointers

    Does anybody have any idea why my output displays the 1d node name for both the 2d nodes "alice" and "joe" to be "bob" when my input is: "Ins alice wife", followed by "Ins joe bob". I'm really stuck on this one.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    struct node2d {
        struct node1d *first;
        char *name;
        struct node2d *down;
    };
    struct node1d {
        struct node1d *next;
        char *name;
    };
    
    
    int print2d (struct node2d *);
    struct node2d *make2dnode(char *, struct node2d *);
    struct node1d *make1dnode2(char *, struct node1d *);
    int dostuff(struct node2d *);
    
    
    
    int main (int argc, char *argv[]) {
    	int i;
    	int x = 0;
        struct node2d *p2d=NULL;
        for (i=argc-1; i>0; i--) {
    		p2d = make2dnode(argv[i], p2d);
    	};
    	dostuff(p2d);
    	dostuff(p2d);
    	print2d(p2d);
    	system("PAUSE");
    
    }
    int dostuff(struct node2d *node){
    	int c;
    	int x=0;
    	char input[1000];
    	struct node2d *p2d = node;
    	printf("Enter command: ");
    	while((c=getchar())!='\n'){
    		if (c==EOF)
    			return 2;
    		input[x] = c;
    		x++;
    	}
    	input[x]= '\0';
    	if(input[0]=='I' && input[1] == 'n' && input[2] == 's' && input[3] == ' '){
    		int i = 4;
    		int x = 0;
    		int y = 0;
    		int z = 0;
    		char node2[1000];
    		char node1[1000];
    		struct node1d *p1d=NULL;
    		while(input[i]!= ' '){
    			node2[x] = input[i];
    			x++;
    			i++;
    		}
    		node2[x]='\0';	
    		i++;
    		while(input[i]!= NULL){
    			node1[z] = input[i];
    			z++;
    			i++;
    		}
    		node1[x]='\0';	
    		while(p2d){
    			if(strcmp((p2d->name), node2)==0){
    				p1d = make1dnode2(node1, p1d);
    				if(p2d->first==NULL|| strcmp(p2d->first->name, p1d->name) == 1){
    					p2d->first=p1d;
    				}
    				else{
    					while(strcmp(p2d->first->name, p1d->name) != 1){
    						p2d->first=p2d->first->next;
    						}
    						p2d->first = p1d;
    						p1d->next= p2d ->first;
    					
    					}
    			}
    			p2d=p2d->down;
    		}
    	}
    	else if(input[0] == 'R' && input[1] == 'e' && input[2] == 'm' && input[3] == ' ')
    		printf("Yess");
    	else{
    		printf("Error");
    		return 1;
    	}
    	system("PAUSE");
    }
    		
    
    	
    
    
    // Make a 2 dimensional node with down pointer next and name name
    struct node2d *make2dnode(char *name, struct node2d *next){
        struct node2d *p2d;
        p2d = (struct node2d *) malloc(sizeof(struct node2d));
        p2d->first = NULL;
        p2d->name = name;
        p2d->down = next;
        return p2d;
    }
    
    int print2d (struct node2d *p2d) {
        int i=0;
    	int x=0;
    	struct node1d *p1d;
        while (p2d) {
    		(i==0) ? printf("The first 2d-node has name: %s\n", p2d->name): printf("The next 2d-node has name: %s\n", p2d->name);
    		i++;
    		p1d = p2d->first;
    	while (p1d) {
    	    printf("\t %s has a 1d-node with name %s\n", p2d->name, p1d->name);
    		x++;
    		p1d = p1d->next;
    	};
    	p2d = p2d->down;
        };
    	printf("The number of 2d nodes is %d\n", i);
    	printf("The number of 1d nodes is %d\n", x);
    	return 0;
    }
    struct node1d *make1dnode2(char *name, struct node1d *next){
    	struct node1d *p1d;
        p1d = (struct node1d *) malloc(sizeof(struct node1d));
        p1d->next = NULL;
        p1d->name = name;
        return p1d;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So I don't see anywhere where you copy the string into the node. You have a line
    Code:
    p1d->name = name;
    which you probably intended to do that, but doesn't (you only have one piece of memory that you keep using over and over again, and each node accumulates a pointer to that same chunk of memory). You probably want to give each node its own piece of memory that it can store a name in, separate from all the others.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    20
    I thought so too, but isn't the line "p1d = (struct node1d *) malloc(sizeof(struct node1d));" in the bottom function supposed to do that?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. That allocates a node. The node has a pointer. The pointer doesn't have any space for it unless you give it some by making it point some place valid. If you make it point at the same buffer, and then overwrite that buffer with different values, all that you're doing is making each node end up with whatever the final value in that buffer is.
    Code:
    struct foo
        char *bar;
    };
    ...
    struct foo *baz;
    ...
    baz = malloc( sizeof *baz );
    ...
    baz->bar = malloc( SOME_SIZE );
    ...
    strcpy( baz->bar, "hello world" );
    ...
    free( baz->bar );
    free( baz );
    That's the general idea. You can't just point baz->bar at some array some place, then keep changing what's in the array, and expect each new copy of your node to have what was in the array at the time you pointed at it. That's not how pointers work. They point at the memory address, and retrieve whatever is currently there when dereferenced.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copying pointers
    By Tom Bombadil in forum C Programming
    Replies: 10
    Last Post: 05-22-2009, 01:26 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  4. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 05:31 PM
  5. hangman need help with strings and pointers
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 08:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21