pointers to pointers question

Discussion in 'C Programming' started by Chad, Mar 26, 2006.

  1. Chad

    Chad Guest

    How come something like:

    #include <stdio.h>

    int main(int argc, char **argv)
    {
    char **arg;
    int i;

    for ( i = 0; argv != NULL; ++i )
    {
    puts(argv);
    }

    for ( arg = argv; *arg != NULL; ++arg )
    {
    puts(*arg);
    }
    return 0;
    }

    doesn't require a explicit & before argv in the for loop, but something
    like

    #include <stdio.h>
    #include <stdlib.h>

    int main(void) {
    char *ptr = "Garbage";

    char **pptr;
    pptr = &ptr;
    printf("The value is: % s\n", *pptr);
    }

    does require an explict &

    Chad
    Chad, Mar 26, 2006
    #1
    1. Advertising

  2. Chad

    santosh Guest

    Chad wrote:
    > How come something like:
    > #include <stdio.h>
    > int main(int argc, char **argv) {
    > char **arg;
    > int i;
    > for ( i = 0; argv != NULL; ++i )
    > {
    > puts(argv);
    > }
    > for ( arg = argv; *arg != NULL; ++arg )
    > {
    > puts(*arg);
    > }
    > return 0;
    > }
    >
    > doesn't require a explicit & before argv in the for loop, but something
    > like
    > #include <stdio.h>
    > #include <stdlib.h>
    > int main(void) {
    > char *ptr = "Garbage";
    > char **pptr;
    > pptr = &ptr;
    > printf("The value is: % s\n", *pptr);
    > }
    >
    > does require an explict &


    Well, in the first case puts() expects a pointer to a null terminated
    array of char and the expression supplies it, while in the second
    example, you'll need the *address* of the pointer, not it's value, to
    initialise pptr and hence & operator must be used.
    santosh, Mar 26, 2006
    #2
    1. Advertising

  3. Chad

    santosh Guest

    Chad wrote:
    > How come something like:
    >
    > #include <stdio.h>
    >
    > int main(int argc, char **argv)
    > {
    > char **arg;
    > int i;
    >
    > for ( i = 0; argv != NULL; ++i )
    > {
    > puts(argv);
    > }
    >
    > for ( arg = argv; *arg != NULL; ++arg )
    > {
    > puts(*arg);
    > }
    > return 0;
    > }
    >
    > doesn't require a explicit & before argv in the for loop, but something
    > like
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(void) {
    > char *ptr = "Garbage";
    >
    > char **pptr;
    > pptr = &ptr;
    > printf("The value is: % s\n", *pptr);
    > }
    >
    > does require an explict &


    In C arr[n] is converted by the compiler to 'arr + n' and the resulting
    address is deferenced. In the second example, you want the address of
    the pointer itself. If you had written:
    pptr = *ptr;
    the compiler would warn about casting an int to pointer type but would
    go ahead and compile. However, during runtime the value stored at the
    address pointed to by ptr, (which in this case is the code for 'G'),
    would be loaded into pptr after any necessary conversion. When you
    deference pptr, your program will invoke undefined behaviour and will
    most likely will crash or be terminated.
    santosh, Mar 26, 2006
    #3
  4. Chad

    Chad Guest

    santosh wrote:
    > Chad wrote:
    > > How come something like:
    > >
    > > #include <stdio.h>
    > >
    > > int main(int argc, char **argv)
    > > {
    > > char **arg;
    > > int i;
    > >
    > > for ( i = 0; argv != NULL; ++i )
    > > {
    > > puts(argv);
    > > }
    > >
    > > for ( arg = argv; *arg != NULL; ++arg )
    > > {
    > > puts(*arg);
    > > }
    > > return 0;
    > > }
    > >
    > > doesn't require a explicit & before argv in the for loop, but something
    > > like
    > >
    > > #include <stdio.h>
    > > #include <stdlib.h>
    > >
    > > int main(void) {
    > > char *ptr = "Garbage";
    > >
    > > char **pptr;
    > > pptr = &ptr;
    > > printf("The value is: % s\n", *pptr);
    > > }
    > >
    > > does require an explict &

    >
    > In C arr[n] is converted by the compiler to 'arr + n' and the resulting
    > address is deferenced. In the second example, you want the address of
    > the pointer itself. If you had written:
    > pptr = *ptr;
    > the compiler would warn about casting an int to pointer type but would
    > go ahead and compile. However, during runtime the value stored at the
    > address pointed to by ptr, (which in this case is the code for 'G'),
    > would be loaded into pptr after any necessary conversion. When you
    > deference pptr, your program will invoke undefined behaviour and will
    > most likely will crash or be terminated.


    Hmmm..... fascinating. Normally it would have taken me a good 3 days to
    make heads or tails out of the responses. I think this has to be the
    first time I've actually been able to understand a response in less
    than 5 hours.
    Chad, Mar 27, 2006
    #4
  5. Chad

    Guest

    the type of pptr is "char **", but the type of "ptr" is "char *", so
    the & before "ptr" is needed.
    , Mar 27, 2006
    #5
  6. Chad

    Default User Guest

    wrote:

    > the type of pptr is "char **", but the type of "ptr" is "char *", so
    > the & before "ptr" is needed.


    That's nice. Too bad it's impossible to tell why you're saying this.
    See below for valuable information.



    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Mar 27, 2006
    #6
    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. Phil
    Replies:
    1
    Views:
    638
    llewelly
    Sep 16, 2003
  2. muser
    Replies:
    3
    Views:
    749
    Ron Natalie
    Sep 18, 2003
  3. A
    Replies:
    3
    Views:
    436
    Alan Kelon
    Oct 29, 2003
  4. Xamalek
    Replies:
    7
    Views:
    679
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    652
Loading...

Share This Page