Loop with pointer arithmetic refuse to stay within boundary in C. Gives me segfault.
Posted
by Fred
on Stack Overflow
See other posts from Stack Overflow
or by Fred
Published on 2010-03-24T06:22:32Z
Indexed on
2010/03/24
6:33 UTC
Read the original article
Hit count: 218
Hi have made this function which is made to replicate an error that I can't get past. It looks like this:
void enumerate(double *c, int size){
while(c < &c[size]){
printf("%lf\n", *c);
c++;
}
}
I have added some printf's in there and it gives me:
Adressof c: 0x100100080, Adressof c + size: 0x1001000a8
I then also print the address of c for each iteration of the loop, it reaches 0x1001000a8 but continues past this point even though the condition should be false as far as I can tell until I get a segfault. If anyone can spot the problem, please tell me, I have been staring at this for a while now.
Thanks.
© Stack Overflow or respective owner