Thread: arrays of strings

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    31

    arrays of strings

    Hi,

    this should be a simple question. How do i initialise an array of strings and then how do i copy strings into the array?

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    If you want an actual array of fixed size, like so:
    Code:
    char foo[5][100]; /* declare 5 "strings" that can fit 100 characters including the null terminator */
    strcpy(foo[0], "This is a string");
    strcpy(foo[1], "This is another string");
    /* ... */
    Or you could declare an array of char pointers then allocate:
    Code:
    char *foo[5];  /* an array of 5 pointers to char */
    foo[0] = malloc(7); /* check malloc succeeded in real code */
    strcpy(foo[0], "potato");
    foo[1] = malloc(4); /* check malloc succeeded in real code */
    strcpy(foo[1], "pea");
    /* ... */

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    31
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  2. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  3. working with strings arrays and pointers
    By Nutka in forum C Programming
    Replies: 4
    Last Post: 10-30-2002, 08:32 PM
  4. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 10:55 AM
  5. Searching arrays for strings
    By Zaarin in forum C++ Programming
    Replies: 14
    Last Post: 09-03-2001, 06:13 PM