I know that arrays in C are just pointers to sequentially stored data. But what differences imply the difference in notation [] and *. I mean in ALL possible usage context. For example:
char c[] = "test";
if you provide this instruction in a function body it will allocate the string on a stack while
char* c = "test";
will point to a data (readonly) segment.
Can you list all the differences between these two notations in ALL usage contexts to form a clear general view.
Actually arrays are equivalent to constant pointers.
Also, char c[] allocates memory for the array, whose base address is c itself. No separate memory is allocated for storing that address.
Writing char *c allocates memory for the string whose base address is stored in c. Also, a separate memory location is used to store c.
According to the C99 standard:
According to the standard declarations…
…are identical. The contents of the arrays are modifiable. On the other hand, the declaration…
…defines p with the type as pointer to constant
char
and initializes it to point to an object with type constant array ofchar
(in C++) with length 4 whose elements are initialized with a character string literal. If an attempt is made to usep
to modify the contents of the array, the behavior is undefined.According to 6.3.2.1 Array subscripting dereferencing and array subscripting are identical:
The differences of arrays vs. pointers are:
These links may be useful to the subject:
This will create an array containing the string test so you can modify/change any character, say
but,
It is a string literal -- it's a const char.
So doing any modification to this string literal gives us segfault. So
is illegal now and gives us segfault.
The whole lot becomes clear if you know that declaring a pointer variable does not create the type of variable, it points at. It creates a pointer variable.
So, in practice, if you need a string then you need to specify an array of characters and a pointer can be used later on.
char []
denotes the type "array of unknown bound of char", whilechar *
denotes the type "pointer to char". As you've observed, when a definition of a variable of type "array of unknown bound of char" is initialised with a string literal, the type is converted to "array[N] of char" where N is the appropriate size. The same applies in general to initialisation from array aggregate:arr is converted to type "array[3] of int".
In a user-defined type definition (
struct
,class
orunion
), array-of-unknown-bound types are prohibited in C++, although in some versions of C they are allowed as the last member of a struct, where they can be used to access allocated memory past the end of the struct; this usage is called "flexible arrays".Recursive type construction is another difference; one can construct pointers to and arrays of
char *
(e.g.char **
,char (*)[10]
) but this is illegal for arrays of unknown bound; one cannot writechar []*
orchar [][10]
(althoughchar (*)[]
andchar [10][]
are fine).Finally, cv-qualification operates differently; given
typedef char *ptr_to_char
andtypedef char array_of_unknown_bound_of_char[]
, cv-qualifiying the pointer version will behave as expected, while cv-qualifying the array version will migrate the cv-qualification to the element type: that is,const array_of_unknown_bound_of_char
is equivalent toconst char []
and not the fictionalchar (const) []
. This means that in a function definition, where array-to-pointer decay operates on the arguments prior to constructing the prototype,is legal; there is no way to make the array-of-unknown-bound parameter non-modifiable.