Thread: Printing pointers of an array

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    44

    Printing pointers of an array

    I am brand new to C, so I've been trying to do some research. This seems to be right by everything I read, but I could use a hand.

    Code:
    char *signedtwobit(char *x, char *y);
    
    int main(void) {
    	char x[]={0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    	char y[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
    	char *p, *q;
    	p=x;
    	q=y;
    	
    	char *s;
    	s=signedtwobit(p,q);
    	int i=0;
    	for (i=0;i<=32;i++) {
    		printf("|%d|, ", *s);
    		s++; 
    	}
    	
    	
    }
    
    char *signedtwobit(char *x, char *y) {
    	char s[33];
    	int i;
    	
    	for (i=31; i>=0; --i) {
    		s[i]=(int)x+(int)y;
    		if (s[i]==2) {
    			s[i]=0;
    			x++;
    			*x++;
    			--x;
    		x=x+1;
    		y=y+1;
    	}
    }
    	
    	char *p=&s[0];
    	return p;
    	
    }

    This is what I get.
    Code:
    |-64|, |-12|, |111|, |5|, |-72|, |16|, |-122|, |4|, |8|, |96|, |-125|, |4|, |8|, |8|, |-50|, |-104|, |-65|, |96|, |27|, |-12|, |-73|, |-64|, |116|, |5|, |-72|, |-64|, |-122|, |4|, |8|, |24|, |-50|, |-104|, |-65|,
    I should get.
    Code:
    |0|, |1|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|, |0|,
    Something is wrong in the signedtwobit function. Because instead of running through the function, I assigned a pointer to the x array and got the correct string.

    I would love any type of help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are returning a pointer to a local variable. Since the local variable goes away when the function is done, you are left with a pointer to nothing in particular.

    Also (int)x casts the pointer to an int, so you might get something in the 1 billion range. It certainly won't be the value of one of the array elements.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    So instead I should pass the char array instead of the pointer?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are passing the array. You had it right up top: *s to get at the innards of the array. It hasn't changed just because you've gone twelve lines down in your program.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    sorry, i meant return the array instead of the pointer.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't return an array.

    If you want this array to be available outside the function, you must manage the memory yourself (say hello to malloc, your new friend).

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    One more quick question. I'm wondering why I can't do this.

    Code:
    	char x[]={0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    	char y[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
    	char *p, *q;
    	p=x;
    	q=y;
    	signedtwobit(p,q);
    
    //says it needs an expression before ']'	
    	x[]={0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    	y[]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    	p=x;
    	q=y;
    	signedtwobit(p,q);
    
    	x[]={1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    	y[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
    	p=x;
    	q=y;
    	signedtwobit(p,q);
    
    
    //Says it expects an expression before '{'
    	x={1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    	y={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    	p=x;
    	q=y;
    	signedtwobit(p,q);
    
    	x={0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    	y={1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    	p=x;
    	q=y;
    	signedtwobit(p,q);

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can never assign to an array. You can only assign to individual slots in an array. (That thing you do at first is "initializing" and can only happen when the memory is first allocated, i.e., when it is declared.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  2. Having trouble printing from an array
    By Sir Andus in forum C Programming
    Replies: 2
    Last Post: 10-30-2006, 01:48 PM
  3. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM

Tags for this Thread