array of strings

This is a discussion on array of strings within the C Programming forums, part of the General Programming Boards category; Hi there! This is my first post and i would first like to apologize for my bad english if there ...

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

    array of strings

    Hi there!

    This is my first post and i would first like to apologize for my bad english if there will be one...

    I have a question about a program which i do not know how to make exactly, but it is a fairly simple program...

    I would like to make an array, where i would save strings into. But i have no idea how to do that. I was thinking of using malloc,...

    Please help me

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If you use malloc for each string, then this is an array of char pointers, eg, for ten strings:
    Code:
    char *strings[10];
    If you use malloc for the whole thing, then you don't need to hardcode the number of strings:
    Code:
    char **strings; // pointer to pointer(s)
    int n = 10;  // can set n dynamically at runtime
    strings = malloc(n*sizeof(char*));
    For both of those you then still need to malloc each string. However, if you are okay with hardcoding both the number of strings and their length, this is fine:

    Code:
    char strings[10][256];
    But don't do that if the array is going to be very large. Eg:
    Code:
    char strings[10000][1000];
    Is ~10 MB and that will blow your stack, so you would have to use malloc in both dimensions.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    4
    so if i would go like:

    Code:
    for (i=0;i<10;i++){
          array[i]=(char*)malloc(10*sizeof(char));
         }
    it would be ok?

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    29
    The way you did it, you would need something else before that.
    Either
    Code:
        char *array[10];
        for (i=0;i<3;i++)
              array[i]=(char*)malloc(10*sizeof(char));
    or
    Code:
        char **array = (char **)malloc(10*sizeof(char *));
        for (i=0;i<3;i++)
              array[i]=(char*)malloc(10*sizeof(char));

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    4
    yes, that was what i would do, at least the first part of it. what is the difference between both of you examples?

    sory if i am asking stupid questions, but i would like to understand...

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    29
    Quote Originally Posted by janeznovak View Post
    yes, that was what i would do, at least the first part of it. what is the difference between both of you examples?

    sory if i am asking stupid questions, but i would like to understand...
    It doesn't actually make a difference when you actually use the array.
    It's more a difference in where exactly the variable in memory is stored, but that doesn't have to concern you too much i guess. Just when using malloc() u have to remember that you release the memory after you're done with free(), since C needs you to take care of that low level stuff by yourself.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by janeznovak View Post
    yes, that was what i would do, at least the first part of it. what is the difference between both of you examples?

    sory if i am asking stupid questions, but i would like to understand...
    As naturegirl points out the difference is where the base array of pointers is created. In her first example it is created on the stack and is thus of fixed size. In her second example it is created on the memory heap and can be resized using realloc(). There is no difference in performance and the syntax to use both is the same.

    The decision between them is pretty simple...
    Q: How many strings do you have to store/load ?
    A: 41 ... use the first version
    A: "I don't know" ... use the second one.
    Last edited by CommonTater; 11-25-2011 at 11:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing an array of strings until array == NULL
    By mikemhz in forum C Programming
    Replies: 10
    Last Post: 11-04-2011, 02:09 PM
  2. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  3. Replies: 2
    Last Post: 04-27-2008, 04:39 AM
  4. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM
  5. Array of Strings
    By mjpars in forum C Programming
    Replies: 8
    Last Post: 08-22-2003, 12:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21