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?
Printable View
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?
If you want an actual array of fixed size, like so:
Or you could declare an array of char pointers then allocate: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");
/* ... */
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");
/* ... */
thanks :)