VERY newbie pointer issue

Discussion in 'C Programming' started by Don, Oct 16, 2003.

1. DonGuest

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?

Don, Oct 16, 2003

2. Andreas KahariGuest

In article <bmlodu\$l06\$-c.dk>, Don wrote:
> 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;
}

--
Andreas Kähäri

Andreas Kahari, Oct 16, 2003

3. Andreas KahariGuest

In article <>, Andreas
Kahari wrote:
[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

--
Andreas Kähäri

Andreas Kahari, Oct 16, 2003
4. disGuest

"Don" <> wrote in message news:bmlodu\$l06\$-c.dk...

> 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.

dis, Oct 16, 2003
5. DonGuest

"dis" <> wrote in message
news:bmlp9b\$in\$1.nb.home.nl...
> "Don" <> wrote in message

news:bmlodu\$l06\$-c.dk...
>
> > 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)?

Don, Oct 16, 2003
6. disGuest

"Don" <> wrote in message news:bmlq6s\$ls1\$-c.dk...

[snip]

> Ahhh thanx a lot all you guys. But if ptr[6] is equivalent to *(ptr+6)

then
> is &ptr[6] equal to (ptr+6)?

Yes indeed

dis, Oct 16, 2003
7. Dan PopGuest

In <bmlodu\$l06\$-c.dk> "Don" <> writes:

>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
--
Dan Pop
DESY Zeuthen, RZ group
Email:

Dan Pop, Oct 16, 2003

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.