C Programming Quiz #2

Discussion in 'Programming/Scripts' started by neothelord, May 3, 2007.

  1. neothelord

    neothelord New Member

  2. nixen

    nixen New Member

    sorry my bad english but i can't belive that i'm studying c++ language for over 2 year at school and i nerver see code like this :
    void main()
    {
    char *s[]={ "miller","miller","filler","filler"};
    char **p;
    p=s;
    printf("%s ",++*p);
    printf("%s ",*p++);
    printf("%s ",++*p);
    }

    can you explain me how it works? thanks :(
     
  3. neothelord

    neothelord New Member

    here s is an array of 4 character pointers. so when you refer an array by base name, it decays to a pointer to the first element . hence basename s is char **.

    so ptr is a pointer to the first element of the array. when you dereference it with a *, you get the value stored in the first element of the array, which is the pointer to the first string "miller". The prefix ++, increments this pointer by 1. since the data type is char *, the pointer is incremented to point the next byte(second char of the string "miller"). and the first printf prints "iller"

    In the second printf postfix ++ has more precedence than *. so it increments ptr to point to second element of s. but remember, this side effect is applicable only after a sequence point. so the *ptr, still yields the value at the first element of s.

    when control comes to the third ptr, ptr is pointing to the second elemtn of s. here we do a normal dereference to get the address of the second string "miller", and increment it by a char. so this also prints "iller".

    The third and fourth string "filler" are really fillers as the name implies.
     
  4. nixen

    nixen New Member

    Thanks :D The pointer are the base of c++ language but in my life i don't use ** pointer, now i understand
     

Share This Page