It depends on your needs.
char str[40] = "Here is my text";
This will contain an array of 40 characters. The first 15 characters will be set according to the specified string. The rest will be set to null. This is useful if you need to change the string later, but be aware that it will not exceed 40 characters (or 39 characters followed by a null terminator, depending on context).
char str[] = "Here is my text";
This is similar to the above example, except that str
now limited to storing 16 characters (15 for a string + null terminator).
char *str = "Here is my text";
str
now a pointer to a string of 15 characters (plus a null delimiter). You yourself cannot change the string, but you can make str
dot somewhere else. In some environments, this does not apply, and you can actually change the contents of the string, but this is not a good idea. If you need to use a pointer and change the line, you can copy it:
char *str = strdup("Here is my text");
But you need free(str)
or your code will be a memory leak.
source share