![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 14
| Printing pointers of an array 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|, 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|, I would love any type of help. |
| TIMBERings is offline | |
| | #2 |
| and the Hat of Guessing 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 | |
| | #3 |
| Registered User Join Date: Oct 2009
Posts: 14
| So instead I should pass the char array instead of the pointer? |
| TIMBERings is offline | |
| | #4 |
| and the Hat of Guessing 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 | |
| | #5 |
| Registered User Join Date: Oct 2009
Posts: 14
| sorry, i meant return the array instead of the pointer. |
| TIMBERings is offline | |
| | #6 |
| and the Hat of Guessing 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 | |
| | #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 | |
| | #8 |
| and the Hat of Guessing 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 | |
![]() |
| Tags |
| array, pointer |
| Thread Tools | |
| Display Modes | |
|
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 |