Thread: Array of Strings

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    Question Array of Strings

    Can somebody tell me how to create an array of strings and load the strings into the array. I know how to use arrays with integers or even floats but not with strings.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    char array[NUM_OF_STRINGS][MAX_LEN_OF_A_STRING];
    And don't forget room for the null terminator.

    then, to populate one element:
    strcpy (array[0], "Some text");

    There are other ways too.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Hi,
    You can use char *names[MAXNAMES] also..
    Saravanan.T.S.
    Beginner.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You can use char *names[MAXNAMES] also..
    Provided you're using string literals and don't intend on making modifications. Otherwise you also want to throw some dynamic memory allocation in there as well. Treating an uninitialized pointer as a string rarely works.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Otherwise you also want to throw some dynamic memory allocation in there as well.
    It's a must to get array of strings work.
    Saravanan.T.S.
    Beginner.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >It's a must to get array of strings work.
    Not if you use an array of arrays as Hammer described.
    My best code is written with the delete key.

  7. #7
    Registered User char's Avatar
    Join Date
    Apr 2002
    Posts
    31
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXNAMES 5
    
    int main(void)
    {
        char buffer[1024], *str_arr[MAXNAMES];
        int i;
    
        for (i = 0; i < MAXNAMES; i++)
        {
    	fgets(buffer, sizeof buffer, stdin);
    
    	str_arr[i] = malloc(strlen(buffer) + 1);
    	if (str_arr == NULL)
    	    perror("malloc"), exit(EXIT_FAILURE);
    
    	strcpy(str_arr[i], buffer);
        }
    
        return EXIT_SUCCESS;
    }

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>if (str_arr == NULL)
    is incorrect, it should be
    >>if (str_arr[i] == NULL)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    Smile

    Thanks everybody. I got Hammer's array of strings version to work.

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