Take the 2-minute tour ×

I am trying to create an array of strings in C. If I use this code:

char (*a[2])[14];

gcc gives me "warning: assignment from incompatible pointer type". What is the correct way to do this?

edit: I am curious why this should give a compiler warning since if I do printf(a[1]);, it correctly prints "hmm".

share|improve this question
Just for the record, char (*a[2])[14] is an array of two pointers to an array of 14 chars. –  avakar Jul 6 '09 at 19:05
I thought it was fourteen pointers to arrays of two chars xD –  fortran Jul 6 '09 at 20:18
add comment

8 Answers

If you don't want to change the strings, then you could simply do

const char *a[2];
a[0] = "blah";
a[1] = "hmm";

When you do it like this you will allocate an array of two pointers to const char. These pointers will then be set to the addresses of the static strings "blah" and "hmm".

If you do want to be able to change the actual string content, the you have to do something like

char a[2][14];
strcpy(a[0], "blah");
strcpy(a[1], "hmm");

This will allocate two consecutive arrays of 14 chars each, after which the content of the static strings will be copied into them.

share|improve this answer
Agreed. But are the '\n' intended ? –  Raphaël Saint-Pierre Jul 6 '09 at 18:56
No, they weren't. Removed. –  Mikael Auno Jul 6 '09 at 19:00
add comment

There are several ways to create an array of strings in C. If all the strings are going to be the same length (or at least have the same maximum length), you simply declare a 2-d array of char and assign as necessary:

strcpy(strs[0], aString); // where aString is either an array or pointer to char
strcpy(strs[1], "foo");

You can add a list of initializers as well:

char strs[NUMBER_OF_STRINGS][STRING_LENGTH+1] = {"foo", "bar", "bletch", ...};

This assumes the size and number of strings in the initializer match up with your array dimensions. In this case, the contents of each string literal (which is itself a zero-terminated array of char) are copied to the memory allocated to strs. The problem with this approach is the possibility of internal fragmentation; if you have 99 strings that are 5 characters or less, but 1 string that's 20 characters long, 99 strings are going to have at least 15 unused characters; that's a waste of space.

Instead of using a 2-d array of char, you can store a 1-d array of pointers to char:

char *strs[NUMBER_OF_STRINGS];

Note that in this case, you've only allocated memory to hold the pointers to the strings; the memory for the strings themselves must be allocated elsewhere (either as static arrays or by using malloc() or calloc()). You can use the initializer list like the earlier example:

char *strs[NUMBER_OF_STRINGS] = {"foo", "bar", "bletch", ...};

although instead of copying the contents of the string constants, you're simply storing the pointers to them. Note that string constants may not be writable; you can reassign the pointer, like so

strs[i] = "bar";
strs[i] = "foo";

but you may not be able to change the string's contents; i.e.,

strs[i] = "bar";
strcpy(strs[i], "foo");

may not be allowed.

You can use malloc() to dynamically allocate the buffer for each string and copy to that buffer:

strs[i] = malloc(strlen("foo") + 1);
strcpy(strs[i], "foo");


char (*a[2])[14];

declares a as a 2-element array of pointers to 14-element arrays of char.

share|improve this answer
Do you then have to free strs[i] when you are done with it? –  Slater Jul 11 '12 at 16:57
@Slater: yes, if it's the result of a malloc call. –  John Bode Sep 7 '12 at 13:00
+1 for categorize it into 2d and 1d, great answer –  yozloy Oct 28 '12 at 1:18
I just want to thank you for this answer, it helped me! –  Destinaxe Nov 24 '13 at 7:42
@AndrewS: The complete answer won't fit into a comment, but basically it's an artifact of how C treats array expressions; under most circumstances, an expression of type T [N] is converted to an expression of type T *, and the value of the expression is the address of the first element. So if you wrote str = "foo", you'd be trying to assign the address of the first character of "foo" to the array str, which doesn't work. See this answer for more details. –  John Bode Feb 7 at 15:33
show 2 more comments

Ack! Constant strings:

const char *strings[] = {"one","two","three"};

If I remember correctly.

Oh, and you want to use strcpy for assignment, not the = operator. strcpy_s is safer, but it's neither in C89 nor in C99 standards.

strcpy(arr[0], "blah");
share|improve this answer
Is that C99? I don't believe it's possible in ANSI C. –  Noldorin Jul 6 '09 at 18:51
Oh... maybe I'm mistaken; not sure what version it is. –  Mark Jul 6 '09 at 18:56
It's possible in both C89 and C99. It also doesn't matter whether it's with const or without it, although the former is preferred. –  avakar Jul 6 '09 at 19:01
strcpy_s is a Microsoft function. It should probably be avoided because it is not in standard C. –  Cromulent Jul 7 '09 at 4:26
strcpy_s and other "safe functions" are standardized as ISO/IEC TR 24731 (it's an ISO published standard and as such isn't available online for free; the most recent draft is open-std.org/jtc1/sc22/wg14/www/docs/n1225.pdf) –  Pavel Minaev Jul 7 '09 at 21:44
show 2 more comments

The string literals are const char *s.

And your use of parenthesis is odd. You probably mean

const char *a[2] = {"blah", "hmm"};

which declares an array of two pointers to constant characters, and initializes them to point at two hardcoded string constants.

share|improve this answer
add comment

Here are some of your options:

char a1[][14] = { "blah", "hmm" };
char* a2[] = { "blah", "hmm" };
char (*a3[])[] = { &"blah", &"hmm" };  // only since you brought up the syntax -

printf(a1[0]); // prints blah
printf(a2[0]); // prints blah
printf(*a3[0]); // prints blah

The advantage of a2 is that you can then do the following with string literals

a2[0] = "hmm";
a2[1] = "blah";

And for a3 you may do the following:

a3[0] = &"hmm";
a3[1] = &"blah";

For a1 you will have to use strcpy even when assigning string literals. The reason is that a2, and a3 are arrays of pointers and you can make their elements (i.e. pointers) point to any storage, whereas a1 is an array of 'array of chars' and so each element is an array that "owns" its own storage (which means it gets destroyed when it goes out of scope) - you can only copy stuff into its storage.

This also brings us to the disadvantage of using a2 and a3 - since they point to static storage (where string literals are stored) the contents of which cannot be reliably changed (viz. undefined behavior), if you want to assign non-string literals to the elements of a2 or a3 - you will first have to dynamically allocate enough memory and then have their elements point to this memory, and then copy the characters into it - and then you have to be sure to deallocate the memory when done.

Bah - I miss C++ already ;)

p.s. Let me know if you need examples.

share|improve this answer
add comment

Or you can declare a struct type, that contains a character arry(1 string), them create an array of the structs and thus a milti element array

typedef struct name
   char name[100]; // 100 character array

   name yourString[10]; // 10 strings
   printf("Enter something\n:);
   // maybe put a for loop and a few print ststements to simplify code
   // this is just for example 

One of the advantages of this over any other method is that this allows you to scan directly into the string without having to use strcpy;

share|improve this answer
add comment


char* strings[3];
strings[0] = "foo";
strings[1] = "bar";
strings[2] = "baz";
share|improve this answer
that's definately not right dude. you created a 3-character string, and you're assigning "foo\0" to the first character. –  Mark Jul 6 '09 at 18:52
@Mark: Ok, it's a typo! I missed out the *. Take it easy on me. :P –  Noldorin Jul 6 '09 at 18:53
posted the same thing, SO rejected my submission. that's a 3 char string, useful for storing words like "is", "if", "it", "of", etc. not too good :( –  KevinDTimm Jul 6 '09 at 18:53
Please put the * by the identifier. It's not part of the type. (Beginners get bit by this a lot.) –  Zifre Jul 6 '09 at 19:25
Just for anyone else reading this, I would like to point out that Bjarne Stroustrup puts the * by the type... –  MirroredFate Mar 29 '13 at 18:40
show 10 more comments

Your code is creating an array of function pointers. Try

char* a[size];


char a[size1][size2];


See wikibooks to arrays and pointers

share|improve this answer
add comment

Your Answer

