#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Jagged arrays using pointers and malloc


    Hello all. I'm a beginner in C and I'm having trouble making a jagged array and understanding how to use it using pointers and malloc.

    I would like to be able to make an array of strings. (I'm making a parseline function.)

    How do I convert

    char words[10][50];
    words[0][0]='h';
    words[0][1]='i';
    words[1][0]='w';
    words[1][1]='o';
    words[1][2]='r';
    words[1][3]='l';
    words[1][4]='d';
    printf("%s \n",words[0]); //Output is hi
    printf("%s \n",words[1]); //Output is world. It's supposed to be but it's printing worldi for some reason.

    in terms of pointers and malloc?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    > //Output is world. It's supposed to be but it's printing worldi for some reason.
    Where are these lines?
    Code:
    words[0][0]='h';
    words[0][1]='i';
    words[0][2]='\0';
    words[1][0]='w';
    words[1][1]='o';
    words[1][2]='r';
    words[1][3]='l';
    words[1][4]='d';
    words[1][5]='\0';
    If you don't end your strings properly with a \0, you're likely to print garbage.

    What kind of dynamic array do you want?
    Code:
    char words[10][50];  // fixed length words, fixed number of words
    char *words[10];     // variable length words, fixed number of words
    char (*words)[50];   // fixed length words, variable number of words
    char **words;        // variable length words, variable number of words
    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. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by salem
    > //Output is world. It's supposed to be but it's printing worldi for some reason.
    Where are these lines?
    Code:
    words[0][0]='h';
    words[0][1]='i';
    words[0][2]='\0';
    words[1][0]='w';
    words[1][1]='o';
    words[1][2]='r';
    words[1][3]='l';
    words[1][4]='d';
    words[1][5]='\0';
    If you don't end your strings properly with a \0, you're likely to print garbage.

    What kind of dynamic array do you want?
    Code:
    char words[10][50];  // fixed length words, fixed number of words
    char *words[10];     // variable length words, fixed number of words
    char (*words)[50];   // fixed length words, variable number of words
    char **words;        // variable length words, variable number of words
    I see. I really want variable length words and number of words because it's based on whatever the user gives.

    Am I supposed to use *(*(words+j)+k) when I'm adding a letter to a word and *(words+j) when I'm trying to print the word? Or since it's a pointer to a pointer of chars I need to iterate to print the whole word?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    Well you would start things off with
    char **words = malloc( numWords * sizeof(char*) );

    Then later
    words[0] = malloc( (strlen(aWord)+1) * sizeof(char) );
    strcpy(words[0], aWord);


    If you don't know numWords in advance, then just make a guess and read up on realloc() so you can extend the array of pointers as needed.

    Comments on this post

    • am3nd0za agrees
    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
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Thank you so much! Your explanation of how to allocate memory for pointers to pointers and the importance of inserting the '\0' was extremely helpful! Everything is working fine now. :)

IMN logo majestic logo threadwatch logo seochat tools logo