pointer array problem?

Discussion in 'C Programming' started by Bill Cunningham, May 18, 2014.

  1. I read and read my text and; well I don't know what going on here. I
    have tried several things. I have to lower myself to asking what's wrong.
    Maybe I'll learn something. This wouldn't even compile. All the different
    things I tried gave all the same errors.

    #include <stdio.h>

    int main()
    {
    char p[3] = { "one", "two", "three" };
    char *pa[3];
    pa[0] = &p[2];
    pa[1] = &p[1];
    pa[2] = &p[0];
    printf("%s %s %s\n", p[0], p[1], p[2]);
    printf("%s %s %s\n", *(pa[0]), *(pa[1]), *(pa[2]));
    return 0;
    }

    p.c: In function 'main':
    p.c:5:5: error: excess elements in char array initializer
    p.c:5:5: error: (near initialization for 'p')
    p.c:5:5: error: excess elements in char array initializer
    p.c:5:5: error: (near initialization for 'p')
    p.c:5:17: warning: initializer-string for array of chars is too long
    [enabled by default]

    I guess the problems is on line 5.

    Bill
     
    Bill Cunningham, May 18, 2014
    #1
    1. Advertisements

  2. Bill Cunningham

    Xavier Roche Guest

    Le 18/05/2014 06:58, Bill Cunningham a écrit :
    char p[3] is an array of char, and may only contain 3 "char", including
    the terminating \0 (such as the string "hi")

    did you mean char* p[3] ?

    or, better,
    const char* p[3] = { ... };

    (string literals are constant, because even if their type is char[],
    modifying it is "undefined" and segfaults on modern systems)

    you are probably confusing "char" and "string". char is a (generally
    8-bit) character.
     
    Xavier Roche, May 18, 2014
    #2
    1. Advertisements

  3. Bill Cunningham

    Borax Man Guest


    The "char p" section is an array of pointers to string literals. You don't
    need to declare the number of elements, but you DO need to specify it is a
    pointer (add the "*").

    Then it is just a matter of making the array of pointers "pa", point to the
    same string as pointer p. Remember, we want the address of the string, not
    the address of the pointer, so don't add "&".


    #include <stdio.h>

    int main()
    {
    char *p[] = { "one", "two", "three" }; /* 3 pointers po
    char *pa[3];
    pa[0] = p[2];
    pa[1] = p[1];
    pa[2] = p[0];
    printf("%s %s %s\n", p[0], p[1], p[2]);
    printf("%s %s %s\n", pa[0], pa[1], pa[2]);
    return 0;
    }
     
    Borax Man, May 18, 2014
    #3
  4. Bill Cunningham

    Bir lau Guest

    Borax Man may give the right method that you want. I want to point that
    you just declare the char * array pa and you have not allocate memory space for it in your program.
     
    Bir lau, May 18, 2014
    #4
  5. Oh yes yes. I did. Where is my head. Yes I meant a string literal not a
    single char.
     
    Bill Cunningham, May 18, 2014
    #5
  6. [snip]
    * Remember, we want the address of the string, not
    so don't add "&".

    The bold type is what I need to concentrate on. There is some info I'm
    missing I can tell in dealing with this. Most of the pointers I've dealt
    with is passing pointers to function parameters. I know you can also in
    dealing with pointers to structs create an array of pointers to several
    instances of a struct type. But I will catch onto that.

    Thanks again for your help.

    B
     
    Bill Cunningham, May 18, 2014
    #6
  7. You could also declare a two-dimensional array of character:

    char p[][10] = {"one","two","three"};

    The second dimension has to be long enough to hold the longest string plus
    NULL terminator. Then you also can write:

    pa[0] = p[2];
    pa[1] = p[1];
    pa[2] = p[0];

    You do *not* need the "&" because p[1] etc. are already pointers to
    character.

    In *your* printf statement:

    printf("%s %s %s\n", *(pa[0]), *(pa[1]), *(pa[2]));

    You do *not* need the "*" in front of the pointer variables. The "%s"
    expects a "pointer to character" parameter to print. Since "pa[0]" was
    assigned the value of "p[2]", they both have the same "pointer to character"
    value. It makes sense that you print them both without using a "*" in front
    to dereference.
     
    Charles Richmond, May 19, 2014
    #7
  8. [...]

    null or '\0' terminator, not NULL terminator. NULL is (a macro that
    expands to) a null *pointer* constant, not a character constant.
     
    Keith Thompson, May 19, 2014
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.