walking through an array of char pointers

Discussion in 'C Programming' started by gaga, Aug 21, 2004.

  1. gaga

    gaga Guest

    I can't seem to get this to work:

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

    int main()
    {
    char *names[3];
    char **np;

    names[0] = "jack";
    names[1] = "jill";
    names[2] = "zack";

    while (**np != '\0') {
    printf("%s\n",*np);
    np++;
    }

    return 0;
    }

    after printing the 3 names, it prints garbage then sometimes seg faults.
    I know i can do it easily using a for loop, but that's not what I am looking
    for. I was under the impression names is actually:

    [] --> "jack\0"
    [] --> "jill\0"
    [] --> "zack\0"
    [] --> \0


    If, that is so, shouldn't I be able to perform the above loop?

    This works:

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

    int main()
    {
    char *names[3];
    char **np;

    names[0] = "jack";
    names[1] = "jill";
    names[2] = "zack";

    while (**np != '\0')
    printf("\%s\n",*(*np)++);
    np++;
    while (**np != '\0')
    printf("\%s\n",*(*np)++);
    np++;
    while (**np != '\0')
    printf("\%s\n",*(*np)++);

    return 0;
    {

    But, I need 3 loops for what I'd like to do in one.
    I do not want to use the fact that I know there are n
    strings in the array. I want to use the fact that each string is
    null terminated then there is a final null terminator.

    any suggestions would be appreciated.

    - gaga
    gaga, Aug 21, 2004
    #1
    1. Advertising

  2. gaga

    -berlin.de Guest

    gaga <> wrote:
    > I can't seem to get this to work:


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


    > int main()
    > {
    > char *names[3];
    > char **np;


    > names[0] = "jack";
    > names[1] = "jill";
    > names[2] = "zack";
    >
    > while (**np != '\0') {


    np hasn't been in initialized at this point. I guess you meant to
    set
    np = names;

    before the start of the loop. But even then there's no reason why
    the loop should stop after printing "zack" because afterwards np
    would get incremented to point _after_ the third array element,
    which probably is some random bit pattern which, when interpreted
    as a char pointer, doesn't point to something where a '\0' charac-
    ter would be stored. So you would need a fourth element of your
    'names' array that would point to an empty string.

    > printf("%s\n",*np);
    > np++;
    > }
    >
    > return 0;
    > }


    You would need something like this instead to get it working:

    int main( void )
    {
    char *names[ ] = { "jack", "jill", "zack", "" };
    char **np = names;

    while ( **np != '\0' )
    printf( "%s\n", *np++ );
    return EXIT_SUCCESS;
    }

    > This works:


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


    > int main()
    > {
    > char *names[3];
    > char **np;


    > names[0] = "jack";
    > names[1] = "jill";
    > names[2] = "zack";
    >
    > while (**np != '\0')
    > printf("\%s\n",*(*np)++);
    > np++;
    > while (**np != '\0')
    > printf("\%s\n",*(*np)++);
    > np++;
    > while (**np != '\0')
    > printf("\%s\n",*(*np)++);
    >
    > return 0;
    > {


    If this version works for you it's just by accident - on my machine it
    crashes immediately. And what's "\%s" and "*(*np)++" supposed to do?

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Aug 21, 2004
    #2
    1. Advertising

  3. gaga

    CBFalconer Guest

    gaga wrote:
    >
    > I can't seem to get this to work:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > int main()
    > {
    > char *names[3];

    4 /* need a final pointer */
    > char **np;
    >

    np = &names; /* initialize */
    > names[0] = "jack";
    > names[1] = "jill";
    > names[2] = "zack";

    names[3] = ""; /* end marker */
    >
    > while (**np != '\0') {
    > printf("%s\n",*np);
    > np++;
    > }
    >
    > return 0;
    > }
    >
    > after printing the 3 names, it prints garbage then sometimes seg
    > faults. I know i can do it easily using a for loop, but that's
    > not what I am looking for. I was under the impression names is


    Try the commented changes/additions above. Simpler is:

    char *names[] = {"jack", "jill", "zack", ""};
    char* *np;

    for (np = &names; **np; np++) printf("%s\n, *np);


    --
    fix (vb.): 1. to paper over, obscure, hide from public view; 2.
    to work around, in a way that produces unintended consequences
    that are worse than the original problem. Usage: "Windows ME
    fixes many of the shortcomings of Windows 98 SE". - Hutchison
    CBFalconer, Aug 21, 2004
    #3
  4. gaga

    Joe Wright Guest

    CBFalconer wrote:
    > gaga wrote:
    >
    >>I can't seem to get this to work:
    >>
    >>#include <stdio.h>
    >>#include <stdlib.h>
    >>#include <string.h>
    >>
    >>int main()
    >>{
    >> char *names[3];

    >
    > 4 /* need a final pointer */
    >
    >> char **np;
    >>

    >
    > np = &names; /* initialize */
    >
    >> names[0] = "jack";
    >> names[1] = "jill";
    >> names[2] = "zack";

    >
    > names[3] = ""; /* end marker */
    >
    >> while (**np != '\0') {
    >> printf("%s\n",*np);
    >> np++;
    >> }
    >>
    >> return 0;
    >>}
    >>
    >>after printing the 3 names, it prints garbage then sometimes seg
    >>faults. I know i can do it easily using a for loop, but that's
    >>not what I am looking for. I was under the impression names is

    >
    >
    > Try the commented changes/additions above. Simpler is:
    >
    > char *names[] = {"jack", "jill", "zack", ""};
    > char* *np;
    >
    > for (np = &names; **np; np++) printf("%s\n, *np);
    >


    char *names[] = {"jack", "jill", "zack", NULL };
    char **np = names;
    while (*np) printf("%s\n", *np++);

    Note that names is an array of pointers to char. 'names' decays to a
    pointer to the array's first element. A pointer to char. So the
    assignment is 'np = names;', not 'np = &names;'.

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Aug 21, 2004
    #4
  5. gaga wrote:

    > I can't seem to get this to work:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > int main()
    > {
    > char *names[3];
    > char **np;
    >
    > names[0] = "jack";
    > names[1] = "jill";
    > names[2] = "zack";
    >
    > while (**np != '\0') {
    > printf("%s\n",*np);
    > np++;
    > }
    >
    > return 0;
    > }
    >
    > after printing the 3 names, it prints garbage then sometimes seg faults.
    > I know i can do it easily using a for loop, but that's not what I am looking
    > for. I was under the impression names is actually:
    >
    > [] --> "jack\0"
    > [] --> "jill\0"
    > [] --> "zack\0"
    > [] --> \0


    That would be true if you had declared an array of four pointers to char
    (count your list) and set the last one to point to an empty string. You
    obviously did not do that.
    char *names[] = {"Jack", "Jill", "Zack", ""};
    Martin Ambuhl, Aug 21, 2004
    #5
  6. gaga

    tigervamp Guest

    (gaga) wrote in message news:<>...
    > I can't seem to get this to work:
    >
    > #include <stdio.h>


    > #include <stdlib.h>
    > #include <string.h>


    You don't need those last two.

    >
    > int main()
    > {
    > char *names[3];
    > char **np;
    >
    > names[0] = "jack";
    > names[1] = "jill";
    > names[2] = "zack";
    >
    > while (**np != '\0') {


    Here is your first problem. You are dereferencing a pointer (np)
    which you haven't assigned to yet. I'm not sure what you are trying
    to do here.

    > printf("%s\n",*np);


    Don't know how this could possibly work...

    > np++;
    > }
    >
    > return 0;
    > }
    >
    > after printing the 3 names, it prints garbage then sometimes seg faults.


    That's because you are trying to access memory pointed to by a pointer
    that hasn't been initialized with a valid location.

    > I know i can do it easily using a for loop, but that's not what I am looking
    > for. I was under the impression names is actually:
    >
    > [] --> "jack\0"
    > [] --> "jill\0"
    > [] --> "zack\0"


    Good so far...

    > [] --> \0


    Nope. "char *names[3];" declares an array of 3 pointers to char,
    that's it. You may be getting confused with the '\0' that is
    automatically added to string literals.

    There are a few ways to do what you want to do. You can keep track of
    how many elements are in the array and use a for loop to iterate over
    them or you can do something like the following which uses a NULL
    pointer as the last element in the array and is in the spirit of what
    I think you were trying to do:

    #include <stdio.h>

    int main(void)
    {
    char *names[4];
    char **np;

    names[0] = "jack";
    names[1] = "jill";
    names[2] = "zack";
    names[3] = NULL;

    np = names;

    while (*np) {
    printf("%s\n",*np);
    np++;
    }

    return 0;
    }


    > If, that is so, shouldn't I be able to perform the above loop?
    >
    > This works:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > int main()
    > {
    > char *names[3];
    > char **np;
    >
    > names[0] = "jack";
    > names[1] = "jill";
    > names[2] = "zack";
    >
    > while (**np != '\0')
    > printf("\%s\n",*(*np)++);
    > np++;
    > while (**np != '\0')
    > printf("\%s\n",*(*np)++);
    > np++;
    > while (**np != '\0')
    > printf("\%s\n",*(*np)++);
    >
    > return 0;
    > {


    There are more problems with this code than I care to comment on, get
    rid of it.

    Rob Gamble
    tigervamp, Aug 21, 2004
    #6
  7. gaga

    pete Guest

    gaga wrote:
    >
    > I can't seem to get this to work:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > int main()
    > {
    > char *names[3];
    > char **np;
    >
    > names[0] = "jack";
    > names[1] = "jill";
    > names[2] = "zack";
    >
    > while (**np != '\0') {
    > printf("%s\n",*np);
    > np++;
    > }
    >
    > return 0;
    > }



    #include <stdio.h>

    int main(void)
    {
    char *names[] = {"jack", "jill", "zack", NULL};
    char **np;

    for (np = names; *np != NULL; ++np) {
    puts(*np);
    }
    return 0;
    }

    --
    pete
    pete, Aug 22, 2004
    #7
  8. gaga

    Joe Wright Guest

    pete wrote:
    > gaga wrote:
    >
    >>I can't seem to get this to work:
    >>
    >>#include <stdio.h>
    >>#include <stdlib.h>
    >>#include <string.h>
    >>
    >>int main()
    >>{
    >> char *names[3];
    >> char **np;
    >>
    >> names[0] = "jack";
    >> names[1] = "jill";
    >> names[2] = "zack";
    >>
    >> while (**np != '\0') {
    >> printf("%s\n",*np);
    >> np++;
    >> }
    >>
    >> return 0;
    >>}

    >
    >
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > char *names[] = {"jack", "jill", "zack", NULL};
    > char **np;
    >
    > for (np = names; *np != NULL; ++np) {
    > puts(*np);
    > }
    > return 0;
    > }


    Ok, printf is too complicated but for is too.

    #include <stdio.h>

    int main(void) {
    char *names[] = {"jack", "jill", "zack", NULL};
    char **np = names;
    while (*np) puts(*np++);
    return 0;
    }

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Aug 22, 2004
    #8
  9. gaga

    gaga Guest

    (tigervamp) wrote in message news:<>...
    > (gaga) wrote in message news:<>...
    > > I can't seem to get this to work:
    > >
    > > #include <stdio.h>

    >
    > > #include <stdlib.h>
    > > #include <string.h>

    >
    > You don't need those last two.
    >
    > >
    > > int main()
    > > {
    > > char *names[3];
    > > char **np;
    > >
    > > names[0] = "jack";
    > > names[1] = "jill";
    > > names[2] = "zack";
    > >
    > > while (**np != '\0') {

    >
    > Here is your first problem. You are dereferencing a pointer (np)
    > which you haven't assigned to yet. I'm not sure what you are trying
    > to do here.
    >
    > > printf("%s\n",*np);

    >
    > Don't know how this could possibly work...
    >
    > > np++;
    > > }
    > >
    > > return 0;
    > > }
    > >
    > > after printing the 3 names, it prints garbage then sometimes seg faults.

    >
    > That's because you are trying to access memory pointed to by a pointer
    > that hasn't been initialized with a valid location.
    >
    > > I know i can do it easily using a for loop, but that's not what I am looking
    > > for. I was under the impression names is actually:
    > >
    > > [] --> "jack\0"
    > > [] --> "jill\0"
    > > [] --> "zack\0"

    >
    > Good so far...
    >
    > > [] --> \0

    >
    > Nope. "char *names[3];" declares an array of 3 pointers to char,
    > that's it. You may be getting confused with the '\0' that is
    > automatically added to string literals.
    >
    > There are a few ways to do what you want to do. You can keep track of
    > how many elements are in the array and use a for loop to iterate over
    > them or you can do something like the following which uses a NULL
    > pointer as the last element in the array and is in the spirit of what
    > I think you were trying to do:
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > char *names[4];
    > char **np;
    >
    > names[0] = "jack";
    > names[1] = "jill";
    > names[2] = "zack";
    > names[3] = NULL;
    >
    > np = names;
    >
    > while (*np) {
    > printf("%s\n",*np);
    > np++;
    > }
    >
    > return 0;
    > }
    >
    >
    > > If, that is so, shouldn't I be able to perform the above loop?
    > >
    > > This works:
    > >
    > > #include <stdio.h>
    > > #include <stdlib.h>
    > > #include <string.h>
    > >
    > > int main()
    > > {
    > > char *names[3];
    > > char **np;
    > >
    > > names[0] = "jack";
    > > names[1] = "jill";
    > > names[2] = "zack";
    > >
    > > while (**np != '\0')
    > > printf("\%s\n",*(*np)++);
    > > np++;
    > > while (**np != '\0')
    > > printf("\%s\n",*(*np)++);
    > > np++;
    > > while (**np != '\0')
    > > printf("\%s\n",*(*np)++);
    > >
    > > return 0;
    > > {

    >
    > There are more problems with this code than I care to comment on, get
    > rid of it.
    >
    > Rob Gamble



    Guys and gals....

    you suggestions have been helpful, much appreciated, and correct.
    But my blunder caused me to not get my point across.
    I did not initialize **np, yes, but that was a copy paste mistake.

    So, I guess where I'm really troubled is here:

    All the solutions were right, but i'm gonna use Joe's here:

    char *names[] = {"jack", "jill", "zack", NULL };
    char **np = names;
    while (*np) printf("%s\n", *np++);


    Ok, that works, but, why is the last NULL needed in names?
    Shouldn't that be done already? Why does this fail?

    char *names[] = {"jack", "jill", "zack"};
    char **np = names;
    while (*np) printf("%s\n", *np++);


    I guess that's what I'm driving at, sorry for the fck up earlier.

    Please correct me if i am wrong, isn't this:

    char c[] = "yes"

    really:

    "yes\0"


    If that is true, shouldn't this:

    char *names[] = {"jack", "jill", "zack"};

    really be this:

    "jack\0", "jill\0", "zack\0", \0

    why isn't a final null terminator appened after an array of pointers?

    Is it a case of, "that's just how it is"?

    a final clarification would be most appreciated.

    thanks again,
    gaga
    gaga, Aug 22, 2004
    #9
  10. gaga

    CBFalconer Guest

    Joe Wright wrote:
    >

    .... snip ...
    >
    > char *names[] = {"jack", "jill", "zack", NULL };
    > char **np = names;
    > while (*np) printf("%s\n", *np++);
    >
    > Note that names is an array of pointers to char. 'names' decays
    > to a pointer to the array's first element. A pointer to char. So
    > the assignment is 'np = names;', not 'np = &names;'.


    I don't think so. names is locally declared, so the type being
    used in the assignment is not subject to decay to a pointer. We
    want a pointer to a char*, which is not the type of 'names',
    because it is still an array. The value of "sizeof names" should
    point this out. That is one reason I separate the *s in the
    declaration:

    char* *np = &names;

    --
    fix (vb.): 1. to paper over, obscure, hide from public view; 2.
    to work around, in a way that produces unintended consequences
    that are worse than the original problem. Usage: "Windows ME
    fixes many of the shortcomings of Windows 98 SE". - Hutchison
    CBFalconer, Aug 22, 2004
    #10
  11. gaga

    -berlin.de Guest

    gaga <> wrote:
    > Please correct me if i am wrong, isn't this:


    > char c[] = "yes"


    > really:


    > "yes\0"


    That creates an array of chars named 'c' and initialized with
    the literal string "yes", i.e it's equivalent to writing

    char c[] = { 'y', 'e', 's', '\0' };

    > If that is true, shouldn't this:


    > char *names[] = {"jack", "jill", "zack"};


    > really be this:


    > "jack\0", "jill\0", "zack\0", \0


    No. A literal string like "zack" automatically has a '\0' at
    the end. But the 'names' array is an array of char pointers.
    So it just consists of 3 char pointers, each initialized to
    point to a literal string.

    > why isn't a final null terminator appened after an array of pointers?


    What would a "null terminator" for an array of char pointers be?
    A pointer to an empty string? A NULL pointer?

    Arrays are never automatically "null teminated". The only case
    where you get an automatic "null terminator" is when you use
    a literal string, i.e. something that's enclosed in double
    quotes. Then the compiler automatically appends a '\0' character
    to the characters you write between the quotes. But a literal
    string isn't an array, you just can use it to e.g. initialize
    an array of chars.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Aug 22, 2004
    #11
  12. gaga

    -berlin.de Guest

    CBFalconer <> wrote:
    > Joe Wright wrote:
    >>

    > ... snip ...
    >>
    >> char *names[] = {"jack", "jill", "zack", NULL };
    >> char **np = names;
    >> while (*np) printf("%s\n", *np++);
    >>
    >> Note that names is an array of pointers to char. 'names' decays
    >> to a pointer to the array's first element. A pointer to char. So
    >> the assignment is 'np = names;', not 'np = &names;'.


    > I don't think so. names is locally declared, so the type being
    > used in the assignment is not subject to decay to a pointer. We


    Locally or not, with

    char **np = names;

    'names' is used in value context and thus decays to a pointer. It's
    equivalent to

    char **np = &names[ 0 ];

    which is just what's needed. On the other hand

    char **np = &names;

    gives you a compiler warning about assignment from incompatible
    pointer type because you try to assign a pointer to an array of
    pointers to a pointer to pointer.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Aug 22, 2004
    #12
  13. gaga

    CBFalconer Guest

    -berlin.de wrote:
    > CBFalconer <> wrote:
    >> Joe Wright wrote:
    >>>

    >> ... snip ...
    >>>
    >>> char *names[] = {"jack", "jill", "zack", NULL };
    >>> char **np = names;
    >>> while (*np) printf("%s\n", *np++);
    >>>
    >>> Note that names is an array of pointers to char. 'names' decays
    >>> to a pointer to the array's first element. A pointer to char. So
    >>> the assignment is 'np = names;', not 'np = &names;'.

    >
    >> I don't think so. names is locally declared, so the type being
    >> used in the assignment is not subject to decay to a pointer. We

    >
    > Locally or not, with
    >
    > char **np = names;
    >
    > 'names' is used in value context and thus decays to a pointer. It's
    > equivalent to
    >
    > char **np = &names[ 0 ];
    >
    > which is just what's needed. On the other hand
    >
    > char **np = &names;
    >
    > gives you a compiler warning about assignment from incompatible
    > pointer type because you try to assign a pointer to an array of
    > pointers to a pointer to pointer.


    I guess I am guilty of fuzzy thinking. Mmm - when was the last
    time I made a mistake in public ...

    --
    fix (vb.): 1. to paper over, obscure, hide from public view; 2.
    to work around, in a way that produces unintended consequences
    that are worse than the original problem. Usage: "Windows ME
    fixes many of the shortcomings of Windows 98 SE". - Hutchison
    CBFalconer, Aug 22, 2004
    #13
  14. gaga

    gaga Guest

    -berlin.de wrote in message news:<>...
    > gaga <> wrote:
    > > Please correct me if i am wrong, isn't this:

    >
    > > char c[] = "yes"

    >
    > > really:

    >
    > > "yes\0"

    >
    > That creates an array of chars named 'c' and initialized with
    > the literal string "yes", i.e it's equivalent to writing
    >
    > char c[] = { 'y', 'e', 's', '\0' };
    >
    > > If that is true, shouldn't this:

    >
    > > char *names[] = {"jack", "jill", "zack"};

    >
    > > really be this:

    >
    > > "jack\0", "jill\0", "zack\0", \0

    >
    > No. A literal string like "zack" automatically has a '\0' at
    > the end. But the 'names' array is an array of char pointers.
    > So it just consists of 3 char pointers, each initialized to
    > point to a literal string.
    >
    > > why isn't a final null terminator appened after an array of pointers?

    >
    > What would a "null terminator" for an array of char pointers be?
    > A pointer to an empty string? A NULL pointer?
    >
    > Arrays are never automatically "null teminated". The only case
    > where you get an automatic "null terminator" is when you use
    > a literal string, i.e. something that's enclosed in double
    > quotes. Then the compiler automatically appends a '\0' character
    > to the characters you write between the quotes. But a literal
    > string isn't an array, you just can use it to e.g. initialize
    > an array of chars.
    > Regards, Jens


    Jens,
    Thanks for your suggestions and perspective. Your responses
    will allow me to properly (finally) explain my source of confusion.

    K&R(ansi) pages 114 and 115.

    on 114 an array of char pointers is depicted as such:

    [ * ]-----> illegal month\0
    [ * ]-----> jan\0
    [ * ]-----> feb\0
    [ * ]-----> mar\0

    which would confirm your remark,
    "But the 'names' array is an array of char pointers. So it just consists of 3
    char pointers, each initialized to point to a literal string."

    and yet, on the very next page, when discussing the main() params, argc
    and argv, argv which is an array of char pointers (or pointer to pointers),
    it is depicted as such:

    argv:
    [ * ]-----> [ * ]-----> echo\0
    [ * ]-----> hello\0
    [ * ]-----> world\0
    [ 0 ]

    Interesting, 0, (or NULL), is appended for us onto the argv array of pointers,
    but not to a locally declared array of pointers.

    allow me to restate my original question...


    why does this work?


    int main(int argc, char *argv[])
    {
    while (*argv)
    printf("\%s\n",*argv++);
    return 0;
    }


    But this doesn't:


    int main()
    {
    char *names[] = {"jack", "jill", "zack"};
    while (*names)
    printf("%s\n",*names++);
    return 0;
    }


    Why is a NULL appended to argv, but not to locally declared
    array of char pointers?

    - gaga
    gaga, Aug 22, 2004
    #14
  15. gaga

    -berlin.de Guest

    gaga <> wrote:
    > K&R(ansi) pages 114 and 115.


    > on 114 an array of char pointers is depicted as such:


    > [ * ]-----> illegal month\0
    > [ * ]-----> jan\0
    > [ * ]-----> feb\0
    > [ * ]-----> mar\0


    > which would confirm your remark,
    > "But the 'names' array is an array of char pointers. So it just consists of 3
    > char pointers, each initialized to point to a literal string."


    > and yet, on the very next page, when discussing the main() params, argc
    > and argv, argv which is an array of char pointers (or pointer to pointers),
    > it is depicted as such:


    > argv:
    > [ * ]-----> [ * ]-----> echo\0
    > [ * ]-----> hello\0
    > [ * ]-----> world\0
    > [ 0 ]


    > Interesting, 0, (or NULL), is appended for us onto the argv array of
    > pointers, but not to a locally declared array of pointers.


    Yes, but the extra NULL pointer isn't there because argv is an array
    of pointers or because it's coming from somewhere else but because,
    according to the requirements, argv must be set up that way (i.e. to
    have a NULL pointer as the last argument). Under some operating
    systems you can execute a new program from within your own and in
    that case you have to assemble argv for the new program yourself.
    And thus you have to create an array of pointers with one more
    element than you want to pass to the new program and have to
    explicitely set that extra pointer to NULL in order to make that
    array an array that can be used as the argv array. No magic involved
    and nothing of that sort (i.e. appending an extra NULL pointer) gets
    done for you automatically. That's exactly the same thing you must
    do with your 'names' array.

    > allow me to restate my original question...


    > why does this work?


    > int main(int argc, char *argv[])
    > {
    > while (*argv)
    > printf("\%s\n",*argv++);


    Because you're guaranteed that argv always has an extra element
    that's set to NULL. It has been set up that way by whatever
    invokes your program.

    > return 0;
    > }


    > But this doesn't:


    > int main()
    > {
    > char *names[] = {"jack", "jill", "zack"};
    > while (*names)
    > printf("%s\n",*names++);
    > return 0;
    > }


    Because your 'names' array isn't argv and hasn't been set up like
    argv would. To do that you have to add the extra NULL pointer at
    the end.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Aug 22, 2004
    #15
  16. gaga

    pete Guest

    Joe Wright wrote:
    >
    > pete wrote:


    > > for (np = names; *np != NULL; ++np) {
    > > puts(*np);
    > > }


    > Ok, printf is too complicated but for is too.


    > char **np = names;
    > while (*np) puts(*np++);


    As points of style,
    I prefer to compare pointers against NULL explicitly,
    I always use compound statements with loops, ifs and elses,
    and I prefer not to have side effects in function arguments.

    --
    pete
    pete, Aug 22, 2004
    #16
  17. gaga

    gaga Guest

    -berlin.de wrote in message news:<>...
    > gaga <> wrote:
    > > K&R(ansi) pages 114 and 115.

    >
    > > on 114 an array of char pointers is depicted as such:

    >
    > > [ * ]-----> illegal month\0
    > > [ * ]-----> jan\0
    > > [ * ]-----> feb\0
    > > [ * ]-----> mar\0

    >
    > > which would confirm your remark,
    > > "But the 'names' array is an array of char pointers. So it just consists of 3
    > > char pointers, each initialized to point to a literal string."

    >
    > > and yet, on the very next page, when discussing the main() params, argc
    > > and argv, argv which is an array of char pointers (or pointer to pointers),
    > > it is depicted as such:

    >
    > > argv:
    > > [ * ]-----> [ * ]-----> echo\0
    > > [ * ]-----> hello\0
    > > [ * ]-----> world\0
    > > [ 0 ]

    >
    > > Interesting, 0, (or NULL), is appended for us onto the argv array of
    > > pointers, but not to a locally declared array of pointers.

    >
    > Yes, but the extra NULL pointer isn't there because argv is an array
    > of pointers or because it's coming from somewhere else but because,
    > according to the requirements, argv must be set up that way (i.e. to
    > have a NULL pointer as the last argument). Under some operating
    > systems you can execute a new program from within your own and in
    > that case you have to assemble argv for the new program yourself.
    > And thus you have to create an array of pointers with one more
    > element than you want to pass to the new program and have to
    > explicitely set that extra pointer to NULL in order to make that
    > array an array that can be used as the argv array. No magic involved
    > and nothing of that sort (i.e. appending an extra NULL pointer) gets
    > done for you automatically. That's exactly the same thing you must
    > do with your 'names' array.
    >
    > > allow me to restate my original question...

    >
    > > why does this work?

    >
    > > int main(int argc, char *argv[])
    > > {
    > > while (*argv)
    > > printf("\%s\n",*argv++);

    >
    > Because you're guaranteed that argv always has an extra element
    > that's set to NULL. It has been set up that way by whatever
    > invokes your program.
    >
    > > return 0;
    > > }

    >
    > > But this doesn't:

    >
    > > int main()
    > > {
    > > char *names[] = {"jack", "jill", "zack"};
    > > while (*names)
    > > printf("%s\n",*names++);
    > > return 0;
    > > }

    >
    > Because your 'names' array isn't argv and hasn't been set up like
    > argv would. To do that you have to add the extra NULL pointer at
    > the end.
    > Regards, Jens


    Jens,
    Thank you. Now I get.
    Thanks for your patience and explanations.
    Thanks to everyone else who contributed as well. Much appreciated.

    - gaga
    gaga, Aug 22, 2004
    #17
  18. gaga

    Old Wolf Guest

    (gaga) wrote:
    > I can't seem to get this to work:
    >
    > char *names[3];
    >
    > names[0] = "jack";
    > names[1] = "jill";
    > names[2] = "zack";
    >
    > I was under the impression names is actually:
    >
    > [] --> "jack\0"
    > [] --> "jill\0"
    > [] --> "zack\0"
    > [] --> \0
    >


    Explain how you think 4 values can fit into 3 memory locations?
    Old Wolf, Aug 23, 2004
    #18
  19. gaga

    Joe Wright Guest

    pete wrote:

    > Joe Wright wrote:
    >
    >>pete wrote:

    >
    >
    >>> for (np = names; *np != NULL; ++np) {
    >>> puts(*np);
    >>> }

    >
    >
    >>Ok, printf is too complicated but for is too.

    >
    >
    >> char **np = names;
    >> while (*np) puts(*np++);

    >
    >
    > As points of style,
    > I prefer to compare pointers against NULL explicitly,
    > I always use compound statements with loops, ifs and elses,
    > and I prefer not to have side effects in function arguments.


    A chacun son gout.

    I seriously prefer 'if (p)' over 'if (p != NULL)'.

    By 'compound statememts' I suppose you mean curly braces. The use of
    curly braces to encompass one statement annoys me.

    Expressing 'foo(a++)' is well defined. That a is incremented is not
    a side effect. It is an explicit part of the language.

    I hope this disagreement doesn't mean we can't play anymore. :=)
    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Aug 23, 2004
    #19
  20. gaga

    pete Guest

    Joe Wright wrote:
    >
    > pete wrote:


    > I prefer not to have side effects in function arguments.


    > Expressing 'foo(a++)' is well defined. That a is incremented is not
    > a side effect. It is an explicit part of the language.


    "side effect" is technical term in C.

    N869
    5.1.2.3 Program execution
    [#2] Accessing a volatile object, modifying an object,
    modifying a file, or calling a function that does any of
    those operations are all side effects, which are changes
    in the state of the execution environment. Evaluation of an
    expression may produce side effects.

    --
    pete
    pete, Aug 23, 2004
    #20
    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. Jeff Elkins

    Walking through a mysql db

    Jeff Elkins, Jun 4, 2005, in forum: Python
    Replies:
    2
    Views:
    359
    wes weston
    Jun 4, 2005
  2. Jeff Elkins

    Re: Walking through a mysql db

    Jeff Elkins, Jun 4, 2005, in forum: Python
    Replies:
    2
    Views:
    434
    Dennis Lee Bieber
    Jun 4, 2005
  3. Thierry Lam

    Walking through directories and files

    Thierry Lam, Sep 16, 2005, in forum: Python
    Replies:
    1
    Views:
    282
    Fredrik Lundh
    Sep 16, 2005
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,015
    Ian Collins
    May 9, 2006
  5. Xeno Campanoli
    Replies:
    3
    Views:
    101
    Xeno Campanoli
    Jun 15, 2007
Loading...

Share This Page