Pointer arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Denis Palmeiro

    #1

    Pointer arrays

    Hi, I've been reading K&R, and I'm a bit confused. In the book,
    they have the following function:

    void writelines(char *lineptr[], int nlines )
    {
    while( nlines-- > 0 )
    printf("%s\n", *lineptr++);
    }

    Now when I initialize:

    char *lines[] = {"Hello", "Hey"};

    and pass it as the first arg in that function, it works exactly
    like expected.

    However, if I try to do the same thing in main:

    int main(void) {

    char *lineptr[] = {"Hello", "Hey"};
    int i = 2;

    while( i-- > 0 )
    printf("%s\n", *lineptr++);

    return 0;
    }

    I get the error:
    test.c:9: error: wrong type argument to increment

    Now, how come I can increment lineptr in writelines, but not in
    main. If it's declared as an argument, is it another type?

    Also, if this is an array, how come it's possible to increment it
    in the writelines function. I thought you can't change what an
    array points to?

    Thanks in advance,
    Denis Palmeiro

  • Martin Ambuhl

    #2
    Re: Pointer arrays

    Denis Palmeiro wrote:[color=blue]
    > Hi, I've been reading K&R, and I'm a bit confused. In the book,
    > they have the following function:
    >
    > void writelines(char *lineptr[], int nlines )
    > {
    > while( nlines-- > 0 )
    > printf("%s\n", *lineptr++);
    > }
    >
    > Now when I initialize:
    > char *lines[] = {"Hello", "Hey"};
    > and pass it as the first arg in that function, it works exactly
    > like expected.
    > However, if I try to do the same thing in main:[/color]
    [color=blue]
    > int main(void) {
    >
    > char *lineptr[] = {"Hello", "Hey"};
    > int i = 2;[/color]
    [color=blue]
    > while( i-- > 0 )
    > printf("%s\n", *lineptr++);
    >
    > return 0;
    > }
    >
    > I get the error:
    > test.c:9: error: wrong type argument to increment
    >
    > Now, how come I can increment lineptr in writelines, but not in
    > main. If it's declared as an argument, is it another type?
    >
    > Also, if this is an array, how come it's possible to increment it
    > in the writelines function. I thought you can't change what an
    > array points to?[/color]

    Please check the FAQ on the difference between a pointer and array.
    Also check the FAQ for what happens when an array is used as an argument
    in a function call.
    It is considered polite to check the FAQ before posting. More
    pointedly, it is considered rude not to have done so.

    Compare your code to that below, which repeats your, in my view
    ill-chosen, games with autodecrements and autoincrements:

    #include <stdio.h>

    void writelines(char *lineptr[], int nlines)
    {
    while (nlines-- > 0)
    printf("%s\n", *lineptr++);
    }

    int main(void)
    {

    char *lines[] = { "Hello", "Hey" }, **decentargumen t;
    int i = 2;

    writelines(line s, 2);
    decentargument = lines;
    while (i-- > 0)
    printf("%s\n", *decentargument ++);

    return 0;
    }

    Comment

    • Joe Wright

      #3
      Re: Pointer arrays

      Denis Palmeiro wrote:[color=blue]
      > Hi, I've been reading K&R, and I'm a bit confused. In the book,
      > they have the following function:
      >
      > void writelines(char *lineptr[], int nlines )
      > {
      > while( nlines-- > 0 )
      > printf("%s\n", *lineptr++);
      > }
      >
      > Now when I initialize:
      >
      > char *lines[] = {"Hello", "Hey"};
      >
      > and pass it as the first arg in that function, it works exactly
      > like expected.
      >
      > However, if I try to do the same thing in main:
      >
      > int main(void) {
      >
      > char *lineptr[] = {"Hello", "Hey"};
      > int i = 2;
      >
      > while( i-- > 0 )
      > printf("%s\n", *lineptr++);
      >
      > return 0;
      > }
      >
      > I get the error:
      > test.c:9: error: wrong type argument to increment
      >
      > Now, how come I can increment lineptr in writelines, but not in
      > main. If it's declared as an argument, is it another type?
      >
      > Also, if this is an array, how come it's possible to increment it
      > in the writelines function. I thought you can't change what an
      > array points to?
      >
      > Thanks in advance,
      > Denis Palmeiro
      >[/color]

      You can't pass an array to a function. Given..

      void writelines(char *lineptr[], int nlines );

      ...the first argument is really 'char **lineptr', a pointer. In the
      body of main()..

      char *lineptr[] = {"Hello", "Hey"};

      ...lineptr is an array, not a pointer. You can't increment an array.

      --
      Joe Wright mailto:joewwrig ht@comcast.net
      "Everything should be made as simple as possible, but not simpler."
      --- Albert Einstein ---

      Comment

      • Jack Klein

        #4
        Re: Pointer arrays

        On Wed, 28 Jul 2004 20:32:02 -0400, "Denis Palmeiro"
        <entitledx@gta. igs.net> wrote in comp.lang.c:

        As Martin already said, read the FAQ for this newsgroup, link in my
        signature, which has a whole chapter on pointers and arrays.
        [color=blue]
        > Hi, I've been reading K&R, and I'm a bit confused. In the book,
        > they have the following function:
        >
        > void writelines(char *lineptr[], int nlines )
        > {
        > while( nlines-- > 0 )
        > printf("%s\n", *lineptr++);
        > }
        >
        > Now when I initialize:
        >
        > char *lines[] = {"Hello", "Hey"};
        >
        > and pass it as the first arg in that function, it works exactly
        > like expected.
        >
        > However, if I try to do the same thing in main:
        >
        > int main(void) {
        >
        > char *lineptr[] = {"Hello", "Hey"};
        > int i = 2;
        >
        > while( i-- > 0 )
        > printf("%s\n", *lineptr++);
        >
        > return 0;
        > }
        >
        > I get the error:
        > test.c:9: error: wrong type argument to increment
        >
        > Now, how come I can increment lineptr in writelines, but not in
        > main. If it's declared as an argument, is it another type?[/color]

        Again, the FAQ has an entire section on pointers and arrays, and the
        relationship between them. Read it.
        [color=blue]
        > Also, if this is an array, how come it's possible to increment it
        > in the writelines function. I thought you can't change what an
        > array points to?[/color]

        The last sentence above certainly indicates a serious misunderstandin g
        of arrays in C, and their relationship with pointers.

        An array is an object, not a pointer. It never points to anything.
        Not at all, not ever. In many circumstances the name of an array is
        automatically converted into a pointer to its first element, but the
        array itself is not a pointer.
        [color=blue]
        > Thanks in advance,
        > Denis Palmeiro[/color]

        --
        Jack Klein
        Home: http://JK-Technology.Com
        FAQs for
        comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
        comp.lang.c++ http://www.parashift.com/c++-faq-lite/
        alt.comp.lang.l earn.c-c++

        Comment

        Working...