Thread: make an array to point to another array

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    241

    make an array to point to another array

    hey guys ,
    I'm trying to create an array of pointers to pointers which will point to array of pointers
    (to strings)

    I tried
    Code:
    int i;
    char *string[]={
    "my name is dave",
    "we like to dance together",
    "sunny day",
    "hello",
    "in the beginning god created heaven and earth",
    "decleration",
    "wroooong"
    };
    
    char **ptr_str[7];
    
    for (i=0;i<7;i++){
        ptr_str[i]=string[i];
    }
    and all sorts of games with ptr_str (*ptr_str,**ptr_str).
    the app keeps crashing , I don't know how to make the array-elements to point to another
    array-elements..
    thanks!

  2. #2
    Registered User MilleniumFalcon's Avatar
    Join Date
    Feb 2014
    Posts
    33
    Quote Originally Posted by Dave11 View Post
    hey guys ,
    I'm trying to create an array of pointers to pointers which will point to array of pointers
    (to strings)

    I tried
    Code:
    int i;
    char *string[]={
    "my name is dave",
    "we like to dance together",
    "sunny day",
    "hello",
    "in the beginning god created heaven and earth",
    "decleration",
    "wroooong"
    };
    
    char **ptr_str[7];
    
    for (i=0;i<7;i++){
        ptr_str[i]=string[i];
    }
    and all sorts of games with ptr_str (*ptr_str,**ptr_str).
    the app keeps crashing , I don't know how to make the array-elements to point to another
    array-elements..
    thanks!
    Let's think of an array like a pointer. Let's ask ourselves, what is a pointer? Well a pointer points to an address in memory. Knowing this, an array should point to addresses in memory where values are stored. In your case, you have a pointer to a pointer that points to a string of characters. To simplify that, each index of the array is pointing to a single string. From that logic, you may deduce that a 'char *' points to a region of memory where characters are stored at. With a 'char **' pointing to a 'char *' that points to a string.

    Now that we have that out of the way, to fix your program, you only need a 'char **' variable to point to the whole array. From there you can access the memory just as you would in your string array. The reason why your declaration doesn't work is because you are trying to point to multiple arrays of strings. If you truly just want to point to the same memory in your string array from a separate array, you can do so by 'char *' variable_name '[x];' and then by copying the references to strings from your initial array.

    Sample code :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main( )
    {
        int offset = 0;
    
    
        char * strings[] =
        {
            "The boy walked towards the girl",
            "C is a beautiful language",
            "How now brown cow"
        };
    
    
        char ** strings2 = strings;
    
    
        printf( "size : %d\n", sizeof( strings ) );
        for ( ; offset < 3; ++offset )
            printf( "%s\n", strings2[offset] );
    
    
        return 0;
    }
    Last edited by MilleniumFalcon; 02-28-2014 at 06:13 PM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In most contexts, an array will be converted to a pointer to its first element. Thus, given an array like this:
    Code:
    int x[7];
    You can write:
    Code:
    int *p = x;
    In MilleniumFalcon's example, strings is an array of 7 pointers to char. Therefore, when you write:
    Code:
    char ** strings2 = strings;
    strings will be converted to a pointer to its first element, i.e., a pointer to a pointer to char, which is exactly what strings2 is.

    That said, I caution about thinking of an array like a pointer. An array is not a pointer. A pointer points to an address in memory, but moreover it has a type, and this type is not an array type. Conceptually, an array does not point to an address in memory, because it is not a pointer, hence it is wrong to say that "in your case, you have a pointer to a pointer that points to a string of characters". Rather, you have an array of 7 pointers to char. Each of these pointers point to the first character of a string, hence colloquially we say that they point to a string, so colloquially we say that you have an array of 7 strings, but these colloquialisms are simplifications for ease of discussion.

    Likewise, it is not true that "you only need a 'char **' variable to point to the whole array", because a char** cannot point to an array. It can only point to a pointer to char. But, because an array of pointers to char is converted to a pointer to a pointer to char, you can get this char** variable to point to the first element of this array of pointers, and hence be able to access the elements of the array as if the char** variable were the array itself.
    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

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, and on a separate note, this:
    Code:
    char *string[]={
    "my name is dave",
    "we like to dance together",
    "sunny day",
    "hello",
    "in the beginning god created heaven and earth",
    "decleration",
    "wroooong"
    };
    should be:
    Code:
    const char *string[] = {
        "my name is dave",
        "we like to dance together",
        "sunny day",
        "hello",
        "in the beginning god created heaven and earth",
        "decleration",
        "wroooong"
    };
    The pointers each point to the first character of a string literal. Modifying a string literal, e.g., by changing string[0][0] to 'b', results in undefined behaviour, so to avoid such a mistake, we declare the pointers to be pointers to const char. Accordingly, you should change the type of strings2 to const char**.

    If you compare to:
    Code:
    char string[][50] = {
        "my name is dave",
        "we like to dance together",
        "sunny day",
        "hello",
        "in the beginning god created heaven and earth",
        "decleration",
        "wroooong"
    };
    Then you'll find one example of where pointers and arrays differ: string would then be an array of 7 arrays of 50 chars, hence assigning 'b' to string[0][0] is fine because it is not an attempted modification of a string literal. Furthermore, it would no longer be correct to write:
    Code:
    char **strings2 = strings;
    because strings would be converted to a pointer to an array of 50 chars, which is not convertible to a pointer to a pointer to char.
    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

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    thanks! I get it now!
    the thumb rule, if I get it correctly , is that if you want something to point at something , you remove one [] but add another *.
    it does make sense since pointer to array has one level less in terms of the array-lairs , but one extra level of pointing .

    also to make a pointer point to array you just have to write
    ptr=array
    without any * or & since they are meaningless because array's name is already a pointer to array[0]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 07-31-2013, 12:15 AM
  2. issue with point to array.
    By mgracecar in forum C Programming
    Replies: 4
    Last Post: 04-25-2012, 09:58 PM
  3. Replies: 5
    Last Post: 12-20-2011, 09:43 PM
  4. point into an array
    By ekarapanos in forum C Programming
    Replies: 1
    Last Post: 04-17-2003, 09:10 AM
  5. How to point a pointer to an array
    By ripper079 in forum C Programming
    Replies: 8
    Last Post: 08-12-2002, 07:19 PM