Reputation: 71
is the array name is actually like typing the address of the first element of the array ?
based on my understanding of the array name
the array name variable isn't a pointer that has it's own memory address and it's content is the address of the first element of the array
but it's a way of typing the address of first element in a plain english name
i will try now to explain it now visually
consider the following code it will be related to the image i'm about to show you
char[] text = "Hey";
char *ptr = text;
and now look at the image
the *ptr
variable has it's own memory address and the content of the pointer is the memory address of H
which is 0101
now the text variable doesn't have it's own memory address when we type text C interprets it to the memory address 0101
so basically text is 0101
am i right ?
Upvotes: 1
Views: 345
Reputation: 310980
For starters this declaration
char[] text = "Hey";
is not a valid C construction.:) It seems you mean
char text[] = "Hey";
An array type and a pointer type are two different types. Consider the following demonstrative program.
#include <stdio.h>
int main(void)
{
char text[] = "Hello World!";
char *ptr = "Hello World!";
printf( "sizeof( text ) = %zu\n", sizeof( text ) );
printf( "sizeof( ptr ) = %zu\n", sizeof( ptr ) );
return 0;
}
Its output might look like
sizeof( text ) = 13
sizeof( ptr ) = 8
Or if you will apply the address of operator & to the variable text like &text
then the type of the expression will be char ( * )[13]
. While applying the operator to the variable ptr
like &ptr
then the type of the expression will be char **
.
Indeed according to the C Standard an array designator is implicitly converted in expressions (except the expressions mentioned above) to a pointer to its firs element.
So you may write
char text[] = "Hey";
char *ptr = text;
The values of the expressions &text
and ptr
(though they have different types) will be equal because they both point to the initial address of the extent of the memory occupied by the array.
The fact that an array designator is converted to a pointer to its first element is related to how the compiler adjusts a function parameter having an array type to the type of pointer to the array element type. That is for example these two function declarations declare the same one function
void f( char s[] );
and
void f( char *s );
Upvotes: 1
Reputation: 134326
Well, almost but not quite.
text
is a variable of type char [4]
, it's an array of char
s. Now, as per the spec, chapter 6.3.2.1/P3
Except when it is the operand of the
sizeof
operator, the_Alignof
operator, or the unary&
operator, or is a string literal used to initialize an array, an expression that has type ''array of type'' is converted to an expression with type ''pointer to type'' that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.
That means,
char * ptr = text;
is the same as writing
char * ptr = &text[0];
That said,
now the text variable doesn't have it's own memory address when we type text C interprets it to the memory address 0101
No, this is wrong. The variable text
has an address (try doing &text
, it'll be of type char (*) [4]
). Now, since an array is a collection of same objects (member object type) in contiguous memory locations, the address of the array is the address of the first element of the array, so while printing the address of the array and the address of the first element of the array will turn out to be same - but remember, they certainly do differ in type.
Upvotes: 2