Thread: How do u write up an array of strings?

  1. #1
    Registered User
    Join Date
    Nov 2001

    How do u write up an array of strings?

    Need some help with this....I don't know how to write up an array of strings
    char mystr[30]="Hello";
    char mystr2[30]="There";

    and i'd like to put those 2 strings in an array so that i can just access it like strarray[0]="Hello"
    is this possible at all? if yes then please help

    note that for this example the number of elements is known, however, plz tell me how to write it up if the number of elements is unknown

  2. #2
    Registered User
    Join Date
    Sep 2001
    #include <stdio.h>
    int main ()
     char * strArray1[] = {"Hello", "There"};
     char strArray2[][30] = {"How's", "it goin'?"};
     return 0;
    This isn't complete, but maybe it's what you're looking for? Please note that there is a difference between strArray1 and 2... the first is an array of char * (which have appropriate memory allocated at declaration, just like char * string = "foo" would). The latter is a two dimensional array.

    If you don't know how many different strings you'll be using, then you'll need to use pointers instead of arrays, just like any case of not knowing how many elements you'll have...
    int main()
     int n = 3;
     int i;
     char** strArray;
     strArray = malloc(sizeof (char*) * n);
     strArray[1] = malloc (sizeof(char) * (strlen(string1) + 1));
     strcpy (strArray[1], string1);
     return 0;
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  2. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 05:03 PM
  3. printing an array of strings
    By linucksrox in forum C Programming
    Replies: 3
    Last Post: 05-11-2004, 03:31 PM
  4. declaring array of strings dynamically!!!!
    By Real in forum C Programming
    Replies: 1
    Last Post: 08-29-2003, 06:59 AM
  5. remove strings from array
    By ipe in forum C Programming
    Replies: 2
    Last Post: 01-12-2003, 04:53 AM