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.