None of them are declaring strings even, are declaring variables that can support strings.
The last one seems strange to me, but it works, if used right, generally it is not used unless it has a specific reason. The others are all correct.
This declares that the variable str*
will be a pointer to characters. Where these characters are is another part of the code’s problem to say, what you need is to then enter the address of string in this variable.
char *str;
The following is not allowed because it has no way to determine the size of the array.
char str[];
It is already changing a little because it already reserves the space for 100 characters (99 useful) and the string will be next to the variable already allocated in stack. The content will be placed next.
char str[100];
In fact the most common is to do so, in the stack or in the static area:
#include <stdio.h>
int main(void) {
char str[] = "teste";
char *strx = "teste";
printf("%s", str);
printf("%s", strx);
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
See more in Char pointer or char array?.
Understand the difference between array and pointer.
And still read:
I’ve seen people start a string using strcpy(), is it recommended to use it? If so, in which cases?
– Luiz Fernando