Thread: Storing multiple string in array

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    41

    Storing multiple string in array

    How can i store two string into an array ? I know the following code doesnt work. How can i modify the code so that it is able to store both the string in array form.

    Code:
    arrayx[0] = "abc";
    arrayx[1] = "def";

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Assuming your array was declared as
    Code:
    char arrayx[2][string_size];
    Or
    Code:
    char *arrayx[2];
    Where each element is dynamically allocated the proper amount of space:
    Code:
    strcpy ( arrayx[0], "abc" );
    strcpy ( arrayx[1], "def" );
    Don't forget to include string.h.
    My best code is written with the delete key.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Or:
    Code:
    char* arrayx[] =
    {
      "abc",
      "def",
      "ghijkl"
    };
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM