How to declare strings in C

c
summerc picture summerc · Jan 4, 2012 · Viewed 435.1k times · Source

Can anyone explain me what is a difference between these lines of code

char *p = "String";
char p2[] = "String";
char p3[7] = "String";

In what case should I use each of the above ?

Answer

fge picture fge · Jan 4, 2012

This link should satisfy your curiosity.

Basically (forgetting your third example which is bad), the different between 1 and 2 is that 1 allocates space for a pointer to the array.

But in the code, you can manipulate them as pointers all the same -- only thing, you cannot reallocate the second.