Thread: Pointing to an array of strings

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    4

    Pointing to an array of strings

    [F]irst post, be gentle;

    Playing around with pointers and strings just to get a grasp on them before I write an assignment. Haven't played with C for over a year now, trying to understand where I've made my big mistake here. Getting the usual "Segmentation fault (core dumped)" error when printf attempts to execute.

    I've searched the forums to no avail (hence my reason for registering). Thanks in advance for any help.

    Code:
    #include<stdio.h>
    
    typedef char * string;
    
    
    int main( int argc, char* argv[]){
        
        int arrayLength = 41;
        string *table;
        table = malloc(arrayLength*sizeof(string));
    
        string **tablet = &table;
        table[40] = "this";
    
    
        printf("%s", *tablet[40]);
    
        return 0;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Let's remove the typedef and see the code
    Code:
    int main( int argc, char* argv[]){
    
        int arrayLength = 41;
        char **table;
        table = malloc(arrayLength*sizeof(char*));
     
        char* **tablet = &table; /* Three pointers!!!! */
        table[40] = "this";
    
    
        printf("%s", *tablet[40]);
    
        return 0;
    }
    I think that you are a little confused and I - std10093 is just a student,which has not the experience of the other forum's users - do not think you are doing it as you should

    First segmentation fault is in these cases caused by accessing memory that does not belong to your program!

    My guess it that you want create an array which will store the string "this" inside it.I would it like this
    Code:
    #include <stdio.h>
    
    int main( int argc, char* argv[]){
        char* p;                              /* Declare a pointer to char */
    
        p = "this";                           /* Set the pointer to string */
        /* Remember that you can modify the pointer, but not the contents  */
        /* of where it points to.Here you can not modify the second letter */
        /* of string "this"                                                */
    
        printf("%c\n",p[1]);              /* I can access the first letter */
    
        p[1]= 'a';                             /* Error. I got a bus error */
    
    
        printf("%s\n",p);  /* Print the string that pointer p points to    */
    
        return 0;
    }
    On the other hand it seems that you wanted to allocate a 2D array of chars, but i don't see why.
    I will make a simple example with type int, because if i make it with char you will lose all the fun
    Remember that a 2D array is an 1D array (like the usual ones),where every element of it, is a pointer to another array
    Code:
    /* Argument n will be the size of our array. */
    /* The array is going to be a square one (n x n) */
    
    int **get(int n) /* Malloc the array */
    {
        int i,**table;
    
        /* Malloc an 1D array,where every element of it is a pointer */
        table=malloc(n*sizeof(int *));
    
        /* Go to every element of the 1D array, and because every   */
        /* element is a pointer,set every one of them to point to      */
        /* an array (1D), where every element of them is just an int */
        /* In the previous malloc they were pointers to int     */
        for(i=0 ; i<n ; i++)
            table[i]=malloc( n*sizeof(int) ); /*    *(table+i) is equivalent to table[i]   */
        return table;
    }
    Hope this helps


    You typedef'ed the char pointer..hmm...i do not know if this is safe in a large program.Maybe you forget that is actually a pointer

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you want to use malloc(), be sure to include <stdlib.h>

    Clarity is a real goal in programming. I would never typedef something like that, but that's my "rule".

    I couldn't figure out what you were doing with tablet.

    You can't assign a string, you need to strcpy it into place, so include <string.h> also, then:

    Code:
    strcpy(destinationString, sourceString); //or "this" for your sourceString
    Where the two strings, do not overlap.
    Last edited by Adak; 11-06-2012 at 07:26 PM.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Thanks std10093, but I think you're misinterpreting what I'm trying to do with this code. I'm completely aware a string in c is simply a pointer to a character, and an array of strings would be a pointer to a pointer to a char. What I'm trying to do is initialize a pointer to an array of strings in order to pass it to another function later in the program, and by using the test-case "this" and the "printf" function, making sure I'm doing it right. The program is far more expansive than the code I've included, but this segment is the only issue I've encountered.

    Thanks for your help nonetheless! Much appreciated.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Quote Originally Posted by Adak View Post
    If you want to use malloc(), be sure to include <stdlib.h>

    Clarity is a real goal in programming. I would never typedef something like that, but that's my "rule".

    I couldn't figure out what you were doing with tablet.
    I'm basically attempting to initialize a pointer to point to my array of "strings". I initially hadn't typedef'd string, but I figured why not.

    This works fine for me
    Code:
    #include<stdio.h>
    
    
    typedef char * string;
    
    
    
    
    int main( int argc, char* argv[]){
        
        int arrayLength = 41;
        string *table;
        table = malloc(arrayLength*sizeof(string));
    
    
        string **tablet = &table;
        table[40] = "this";
    
    
    
    
        printf("%s", table[40]); // changed to print from table
    
    
        return 0;
    }
    I'm just having trouble accessing the array from the tablet pointer. (Using this pointer to pass the "table" array to a function later in the program, in order for changes to be global, rather than specific to the function they're made within)

    BTW I have included stdlib as well as several other headers but forgot to include all of them in the post.
    Last edited by c9e51; 11-06-2012 at 07:41 PM. Reason: Grammer and more specifics

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Look this over:

    Code:
    #include<stdio.h>
    #include <string.h>  
    #include <stdlib.h>     //malloc
    
    #define SIZE 41
    
    
    int main( int argc, char* argv[]){
        int i;
        char **table;
        table = malloc(SIZE * sizeof(char*));
    
        for(i=0;i<SIZE;i++) {
            table[i]=malloc(SIZE); //
        }
       strcpy(table[0], "this");
       strcpy(table[40], "is fun");
    
       printf("%s \n%s\n\n",table[0],table[40]);
    
       for(i=0;i<SIZE;i++) {
          free(table[i]);
       }
       free(table);
    
       return 0;
    }

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Quote Originally Posted by Adak View Post
    Look this over:

    Code:
    #include<stdio.h>
    #include <string.h>  
    #include <stdlib.h>     //malloc
    
    #define SIZE 41
    
    
    int main( int argc, char* argv[]){
        int i;
        char **table;
        table = malloc(SIZE * sizeof(char*));
    
        for(i=0;i<SIZE;i++) {
            table[i]=malloc(SIZE); //
        }
       strcpy(table[0], "this");
       strcpy(table[40], "is fun");
    
       printf("%s \n%s\n\n",table[0],table[40]);
    
       for(i=0;i<SIZE;i++) {
          free(table[i]);
       }
       free(table);
    
       return 0;
    }

    I see what you're doing, and my array is functioning correctly as highlighted in a previous post, my pointer to this array however is not doing what I'd like it to do. Do you think your for loop might be a little bit redundant as the space should already have been allocated in the malloc before it?
    Last edited by c9e51; 11-06-2012 at 07:56 PM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by c9e51 View Post
    I see what you're doing, and my array is functioning correctly as highlighted in a previous post, my pointer to this array however is not doing what I'd like it to do. Do you think your for loop might be a little bit redundant as the space should already have been allocated in the malloc before it?
    No redundancy that I see. We have a 2D array, so there's a malloc() needed for the outer D of pointers, and the other D needs a loop, because each of the second D arrays, needs to have space allocated for it.

    A better test would be to put in a longer string or two:
    Code:
       strcpy(table[0], "Edelweis, edelweis ");
       strcpy(table[40], "every morning you greet me");
    Because a lot of time you can NOT allocate space at all, and still enter up to 5-10 char's, and not crash the program.

    (Don't ask me how I know this! )


    Note that the two malloc's are not necessarily allocating the same size (depending on the size of char pointers on your system).

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    Quote Originally Posted by c9e51 View Post
    [F]irst post, be gentle;

    Playing around with pointers and strings just to get a grasp on them before I write an assignment. Haven't played with C for over a year now, trying to understand where I've made my big mistake here. Getting the usual "Segmentation fault (core dumped)" error when printf attempts to execute.

    I've searched the forums to no avail (hence my reason for registering). Thanks in advance for any help.

    Code:
    #include<stdio.h>
    
    typedef char * string;
    
    
    int main( int argc, char* argv[]){
        
        int arrayLength = 41;
        string *table;
        table = malloc(arrayLength*sizeof(string));
    
        string **tablet = &table;
        table[40] = "this";
    
    
        printf("%s", *tablet[40]);
    
        return 0;
    }
    Write
    printf("%s", (*tablet)[40]);
    instead of
    printf("%s", *tablet[40]);

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by c9e51 View Post
    I'm completely aware a string in c is simply a pointer to a character, and an array of strings would be a pointer to a pointer to a char.
    Your belief that you are "completely aware", I suspect, is a large part of your problem. Because it is flat out wrong.

    A string in C is not simply a pointer to a character. A string is an array of characters in which the end, by convention, is the first character in the array found with value zero.

    A pointer and an array are very different things in C. That is true for anything, not just characters. But a pointer to a pointer to a char is not an array of strings. There are some contexts in which an array and a pointer can be treated as if they are the same. And other contexts where the fact they are different things becomes readily apparent.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointing to the next character in an array?
    By steez in forum C Programming
    Replies: 8
    Last Post: 06-01-2011, 08:44 PM
  2. array and pointing randomly
    By gloworm in forum C Programming
    Replies: 4
    Last Post: 04-12-2010, 12:03 PM
  3. pointing to an array
    By gloworm in forum C Programming
    Replies: 4
    Last Post: 04-11-2010, 11:46 AM
  4. Replies: 6
    Last Post: 11-03-2009, 01:23 PM
  5. passing/pointing to a 2d array
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 10-28-2005, 10:16 PM