C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-10-2009, 09:45 PM   #1
Registered User
 
Join Date: Oct 2009
Posts: 14
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.
TIMBERings is offline   Reply With Quote
Old 10-10-2009, 09:50 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 10-10-2009, 10:34 PM   #3
Registered User
 
Join Date: Oct 2009
Posts: 14
So instead I should pass the char array instead of the pointer?
TIMBERings is offline   Reply With Quote
Old 10-10-2009, 10:38 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 10-10-2009, 10:41 PM   #5
Registered User
 
Join Date: Oct 2009
Posts: 14
sorry, i meant return the array instead of the pointer.
TIMBERings is offline   Reply With Quote
Old 10-10-2009, 10:53 PM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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).
tabstop is offline   Reply With Quote
Old 10-10-2009, 11:26 PM   #7
Registered User
 
Join Date: Oct 2009
Posts: 14
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);
TIMBERings is offline   Reply With Quote
Old 10-10-2009, 11:35 PM   #8
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.)
tabstop is offline   Reply With Quote
Reply

Tags
array, pointer

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
pass a 2d array to function which takes an array of pointers as argument? alcaseltz C Programming 1 10-21-2007 07:44 AM
Having trouble printing from an array Sir Andus C Programming 2 10-30-2006 01:48 PM
Array of struct pointers - Losing my mind drucillica C Programming 5 11-12-2005 11:50 PM
Help with an Array omalleys C Programming 1 07-01-2002 08:31 AM
Array of pointers for dynamically allocating class instances sh0x C++ Programming 4 09-12-2001 02:05 PM


All times are GMT -6. The time now is 11:55 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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