Thread: point to an array of pointers

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    30

    point to an array of pointers

    Hi all,

    this is my first post.
    i am sorry if i did something wrong.
    i would start with an example.
    i have a multi dimentioal array
    Code:
    in multi[3][4];
    the name multi without brackets is a pointer to the first element (array of 4 elements). However i cannot increment it to point to the next element, because multi is pointer constant.

    to do that i have to define a pointer variable, a pointer that can point to an array of 4 elements:
    Code:
    int (*ptr)[4];
    ptr = multi.
    now i cant use some pointer arithmetik (ptr++).

    now to my question.
    i have the following :
    Code:
    char *message[3] = { “One", "two","three"};
    to printf the first string i can use :
    message[0], or *message

    second string :
    message[1] or *(message +1) ...

    but here as well i cannot increment the pointer message, because it's again a pointer constant.
    and i dont know how to define a pointer variable (which can be incremented and decremented) and assign message to it so that i can do the following:
    Code:
    for(int i = 0; i < 3; i++)
    printf("%s ",*ptr++)
    i tried this and other things...didn't work
    Code:
    char (*ptr)[];
    ptr = message;
    thanks in advance for your help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Scatman
    the name multi without brackets is a pointer to the first element (array of 4 elements). However i cannot increment it to point to the next element, because multi is pointer constant.
    No, the name multi without brackets is an array of 3 arrays of 4 ints. In most contexts, it is converted to a pointer to an array of 4 ints, but that is not what it is, and that is why you cannot increment it as you cannot assign to an array even if you could assign to its elements (i.e., the array itself is not an lvalue).

    Quote Originally Posted by Scatman
    to do that i have to define a pointer variable, a pointer that can point to an array of 4 elements:
    Code:
    int (*ptr)[4];
    ptr = multi.
    now i cant use some pointer arithmetik (ptr++).
    But you can do pointer arithmetic:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int multi[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}};
        int (*ptr)[4] = multi;
        printf("%d\n", (*ptr)[0]);
        ptr++;
        printf("%d\n", (*ptr)[0]);
        return 0;
    }
    You will see 0 and then 4 printed because the increment of ptr caused it to point from the first array element of multi to the second.

    Quote Originally Posted by Scatman
    and i dont know how to define a pointer variable (which can be incremented and decremented) and assign message to it so that i can do the following:
    message is an array of 3 pointers to char. Therefore, in most contexts, it will be converted to a pointer to a pointer to char. Hence, you could write:
    Code:
    char **ptr = message;
    for (int i = 0; i < 3; i++)
        printf("%s ", *ptr++);
    By the way, because the pointers to char will point to the first elements of string literals, you should declare them pointers to const char:
    Code:
    const char *message[3] = {"One", "two", "three"};
    const char **ptr = message;
    This will help to warn you in case you make the mistake of trying to modify a string literal.
    Last edited by laserlight; 11-21-2018 at 05:52 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Some examples.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main ( ) {
        char *message[3] = { "One", "two","three"};
        char **ptr = message;
        for(int i = 0; i < 3; i++)
            printf("%s ",*(ptr++));
        printf("\n");
        char multi[3][6] = {
            { "One" },
            { "two" },
            { "three" }
        };
        char (*mp)[6] = multi;
        for(int i = 0; i < 3; i++)
            printf("%s ",*(mp++));
        printf("\n");
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2018
    Posts
    30
    now i cant use some pointer arithmetik (ptr++).
    sorry a typo...i meant.. I CAN use pointer arithmetik

  5. #5
    Registered User
    Join Date
    Nov 2018
    Posts
    30
    Thanks laserlight and Salem,

    yeah it works perfect.
    however i still don't really understand this :

    Therefore, in most contexts, it will be converted to a pointer to a pointer to char. Hence, you could write:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of pointers to point to another array of pointers
    By DaRealFonz in forum C++ Programming
    Replies: 5
    Last Post: 05-17-2018, 07:26 PM
  2. Replies: 5
    Last Post: 12-20-2011, 09:43 PM
  3. Array of pointers to point objects
    By totalfreeloader in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2003, 09:26 AM
  4. Replies: 5
    Last Post: 09-10-2003, 09:30 PM
  5. Replies: 2
    Last Post: 01-29-2003, 03:32 PM

Tags for this Thread