VERY newbie pointer issue

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

  1. Don

    Don Guest

    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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. 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
    #3
  4. Don

    dis Guest

    "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
    #4
  5. Don

    Don Guest

    "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
    #5
  6. Don

    dis Guest

    "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
    #6
  7. Don

    Dan Pop Guest

    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
    #7
    1. Advertising

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.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    517
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,717
    Merrill & Michele
    Oct 19, 2004
  3. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    466
    jacob navia
    Oct 17, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    541
    Dave Thompson
    Feb 14, 2005
  5. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,376
Loading...

Share This Page