Thread: array of strings

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    array of strings

    say I want to have an array of strings that I don't know how big the size if will be, can I do this??

    Code:
    char ** array;
    array[0] = malloc(strlen(words) + 1);
    strcpy(array[0], words);
    and by the way say that I know I am going to have 3 strings in the array.. do I have to know how big is each of the strings to do this?
    Last edited by -EquinoX-; 04-13-2008 at 02:44 PM.

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    anyone?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    First you need to allocate your array and then what you have should work. You may also have a strdup() function that is rather handy in these circumstances.
    Code:
    array = malloc(sizeof(char *) * numstrings);
    array[0] = strdup(words);

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    well how about every time I want to write a string into an array, I make the array size bigger

    say I want to put the string words

    so I do:

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

    then I want to add another string which is wordy

    array = malloc(sizeof(array) + sizeof(char*)'
    array[1] = malloc(strlen(wordy) + 1);
    strcpy(array[1], wordy);

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    well actually the main question here is

    what would happen if I do a malloc twice

    say the following:

    Code:
    array = malloc(sizeof(char *) * 2);
    array = malloc(sizeof(char *) * 3);
    would it erase or destroy the value inside the array before or would it just make the size of the array bigger without touching any of the value in the array..

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    my problem is actually this, I have an array of strings and I am not sure of the size of it because I don't know yet how many strings are going to be put inside,

    Say my array is called array, and I have a string called test

    char** array = NULL;
    char* test = "My name is John and I love to surf"

    The goal here is to put every words into the array.. the hard way to do is to first traverse through the string and see how many words are there and then do a malloc on the array.. but is there another way to do this easily?

    Here's what I want in the final result
    array[0] = "My";
    array[1] = "name";
    array[2] = "is";
    array[3] = "John";

    etc...

    and the number of string varies.. it depends on what the user enters

    another dumb thing to do would to just malloc a huge amount of array say 10000000, so we do a malloc for 10000000 strings and hope that the input from user does not exceeds 10000000 strings.,.. but I don't want to do that..

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by -EquinoX- View Post
    well actually the main question here is

    what would happen if I do a malloc twice

    say the following:

    Code:
    array = malloc(sizeof(char *) * 2);
    array = malloc(sizeof(char *) * 3);
    would it erase or destroy the value inside the array before or would it just make the size of the array bigger without touching any of the value in the array..
    That would leak the old stuff and set aside room for the new.

    There is such a thing as realloc.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    any other way to do this without realloc?

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    and using realloc do I have to free the old pointer?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    realloc dumps the old memory for you (unless of course the new allocation fails).

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay and any other alternative of doing this without realloc?

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    realloc would be better because it is capable of growing the memory allocation but you could allocate some new memory, copy the old pointers into the new array then free the old array.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay an off topic question, how do you compare if two pointers are equal?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If two pointers are equal, or if what they point to is the same? If you want to know whether two pointers point at the same memory address, you use ==.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay..thanks and say I have the following struct:

    Code:
    struct _rule_t_{
      char** dependency;
      char** buildcmd;
      char* target;
    };
    
    typedef struct _rule_t_ rule_t;
    
    /*a struct as a representation of a list*/
    struct _list_t_ {
      rule_t  *rule;
      struct _list_t_ *next;
    };
    
    typedef struct _list_t_ list_t;
    if I do this:

    list_t temp;
    temp->rule->target

    will this give me the value of target? or does it just give me the pointer of target?

    if I want to get the pointer of target I just use '.' right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intializing an array of strings
    By doubty in forum C Programming
    Replies: 4
    Last Post: 06-19-2009, 12:59 PM
  2. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  4. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 05:03 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM