VERY newbie pointer issue

D

Don

If I have

unsigned char mytest[10];
mytest[0] = 0x1;mytest[1] = 0x2;mytest[2] = 0x3;
mytest[3] = 0x4;mytest[4] = 0x5;mytest[5] = 0x6;
mytest[6] = 0x7;mytest[7] = 0x8;mytest[8] = 0x9;
mytest[9] = 0x10;

and

unsigned char* ptr;

ptr = mytest;

and then print *(ptr+5) then I would get 0x6.
But what if I used the syntax and printed:
*(ptr+5+1) would I then get 0x7? Or is this interpreted otherwize?
 
A

Andreas Kahari

If I have

unsigned char mytest[10];
mytest[0] = 0x1;mytest[1] = 0x2;mytest[2] = 0x3;
mytest[3] = 0x4;mytest[4] = 0x5;mytest[5] = 0x6;
mytest[6] = 0x7;mytest[7] = 0x8;mytest[8] = 0x9;
mytest[9] = 0x10;

and

unsigned char* ptr;

ptr = mytest;

and then print *(ptr+5) then I would get 0x6.
But what if I used the syntax and printed:
*(ptr+5+1) would I then get 0x7? Or is this interpreted otherwize?

Did you try it?


*(ptr + 5) is equivalent to ptr[5] (and to 5[ptr]).

*(ptr + 5 + 1) is equivalent to ptr[5 + 1] (and to (5 + 1)[ptr]
or 5[ptr + 1] or 1[ptr + 5]).


Try this:

#include <stdio.h>

int main(void)
{
char msg[] = "abcdef";
char *p = msg;

printf("p[2] = %c\n", p[2]);
printf("2[p] = %c\n", 2[p]);
printf("p[2 + 3] = %c\n", p[2 + 3]);
printf("2[3 + p] = %c\n", 2[3 + p]);
printf("(2 + 3)[p] = %c\n", (2 + 3)[p]);
printf("*(p + 2 + 3)[p] = %c\n", *(p + 2 + 3));

return 0;
}
 
A

Andreas Kahari

[cut]
#include <stdio.h>

int main(void)
{
char msg[] = "abcdef";
char *p = msg;

printf("p[2] = %c\n", p[2]);
printf("2[p] = %c\n", 2[p]);
printf("p[2 + 3] = %c\n", p[2 + 3]);
printf("2[3 + p] = %c\n", 2[3 + p]);
printf("(2 + 3)[p] = %c\n", (2 + 3)[p]);
printf("*(p + 2 + 3)[p] = %c\n", *(p + 2 + 3));

Typo. Should have said "*(p + 2 + 3)" in the format string as
well of course.
return 0;
}



Cheers,
Andreas
 
D

dis

Don said:
If I have

unsigned char mytest[10];
mytest[0] = 0x1;mytest[1] = 0x2;mytest[2] = 0x3;
mytest[3] = 0x4;mytest[4] = 0x5;mytest[5] = 0x6;
mytest[6] = 0x7;mytest[7] = 0x8;mytest[8] = 0x9;
mytest[9] = 0x10;

and

unsigned char* ptr;

ptr = mytest;

and then print *(ptr+5) then I would get 0x6.

Yes, *(ptr+5) yields the same value as ptr[5] which is 0x6 in this case.
But what if I used the syntax and printed:
*(ptr+5+1) would I then get 0x7?

Yes, *(ptr+5+1) yields the same value as ptr[6] which is 0x7 in this case.
 
D

Don

dis said:
If I have

unsigned char mytest[10];
mytest[0] = 0x1;mytest[1] = 0x2;mytest[2] = 0x3;
mytest[3] = 0x4;mytest[4] = 0x5;mytest[5] = 0x6;
mytest[6] = 0x7;mytest[7] = 0x8;mytest[8] = 0x9;
mytest[9] = 0x10;

and

unsigned char* ptr;

ptr = mytest;

and then print *(ptr+5) then I would get 0x6.

Yes, *(ptr+5) yields the same value as ptr[5] which is 0x6 in this case.
But what if I used the syntax and printed:
*(ptr+5+1) would I then get 0x7?

Yes, *(ptr+5+1) yields the same value as ptr[6] which is 0x7 in this case.


Ahhh thanx a lot all you guys. But if ptr[6] is equivalent to *(ptr+6) then
is &ptr[6] equal to (ptr+6)?
 
D

Dan Pop

In said:
If I have

unsigned char mytest[10];
mytest[0] = 0x1;mytest[1] = 0x2;mytest[2] = 0x3;
mytest[3] = 0x4;mytest[4] = 0x5;mytest[5] = 0x6;
mytest[6] = 0x7;mytest[7] = 0x8;mytest[8] = 0x9;
mytest[9] = 0x10;

and

unsigned char* ptr;

ptr = mytest;

and then print *(ptr+5) then I would get 0x6.
But what if I used the syntax and printed:
*(ptr+5+1) would I then get 0x7?

What else? Strictly speaking, your expression is parsed as *((ptr+5)+1)
which means that the next byte after ptr+5 (i.e. the one at address ptr+6)
is accessed.
Or is this interpreted otherwize?

No, it is interpreted according to the rules governing the binary +
operator. Even if you choose the other parsingi: *(p+(5+1)), the result
is the same.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top