Difference Between char* s and char s[] and char s[n]
char* s;
Create a character pointer s (which is uninitialized).
You can change the value of s. i.e. You can make it point to something else.
sizeof(s) == sizeof(char*).
char s[];
Invalid as a stand-alone variable! This is not valid C++ or C. (See below for valid usage inside a struct or function.)
char s[5];
Create an array s of 5 characters. The array elements are uninitialized.
You can’t change the value of s. i.e. You can’t make it point to something else.
sizeof(s) == 5 because there are 5 characters in the array.
struct MyStruct
{
char* s;
};
Declare a struct type containing a character pointer s.
In any variable of this type, the element s is uninitialized.
You can change the value of s. i.e. You can make it point to something else.
sizeof(MyStruct) == sizeof(char*) because s is a variable of type char*.
struct MyStruct
{
char s[];
};
Declare a struct containing an array s of characters of unknown length. i.e. The variable s is merely another name for the first memory location in the struct.
You can’t change the value of s. i.e. You can’t make it point to something else.
sizeof(MyStruct) == 0 because s is merely a name for a memory location, not a variable.
int func(char* s) { ... };
int func(char s[]) { ... };
Declare a function taking a char* and returning an int.
There is no difference between these two function declarations.