How come an array's address is equal to its value in C?
The name of an array usually evaluates to the address of the first element of the array, so array
and &array
have the same value (but different types, so array+1
and &array+1
will not be equal if the array is more than 1 element long).
There are two exceptions to this: when the array name is an operand of sizeof
or unary &
(address-of), the name refers to the array object itself. Thus sizeof array
gives you the size in bytes of the entire array, not the size of a pointer.
For an array defined as T array[size]
, it will have type T *
. When/if you increment it, you get to the next element in the array.
&array
evaluates to the same address, but given the same definition, it creates a pointer of the type T(*)[size]
-- i.e., it's a pointer to an array, not to a single element. If you increment this pointer, it'll add the size of the entire array, not the size of a single element. For example, with code like this:
char array[16];
printf("%p\t%p", (void*)&array, (void*)(&array+1));
We can expect the second pointer to be 16 greater than the first (because it's an array of 16 char's). Since %p typically converts pointers in hexadecimal, it might look something like:
0x12341000 0x12341010
Address of array vs. address of array[0] - C language
&mallocbuff
is the address of the named variable mallocbuff
. &mallocbuff[0]
is the address of the first element in the buffer pointed to by mallocbuff
, that you allocated with malloc()
.
How to get the address of an array pointer?
Isn't arr a pointer points to an int array?
No, arr
isn't a pointer at all. arr
is an array. If you take the address of arr
, what you get is a pointer to an array, not a pointer to a pointer. This would work:
int arr[8] = {1, 2, 3, 4, 5, 6, 7, 8};
int (*pp)[8] = &arr;
// cleaner by using a type alias:
using Arr8 = int[8];
Arr8* pp = &arr;
If you want a pointer to pointer, then you must first create a pointer to which you could point at:
int* p = arr; // same as = &arr[0]
int** pp = &p;
But when I do printf("arr: %x\n", arr);. It actually prints out the starting address of arr. Doesn't that mean it is a variable stores the address of the array?
It does not mean that. arr
is the array; it doesn't store the address of the array, and the type of the variabe isn't a pointer.
When you pass an array as a variadic argument, it will implicitly convert to a pointer to first element. That is the same implicit conversion that happens in the example above: int* p = arr;
.
Note that %x
format specifier requires that the argument is of type int
(or smimilar). int*
(which is the resulting type of the implicit conversion) is the wrong type, and hence the behaviour of the program will be undefined.
How do you get the address of array elements given the address of the array itself in C?
If 0x00006ffd29c78e70
is the address of a valid array of int
s then you can assign it to a pointer:
int *p = (int*)0x00006ffd29c78e70;
then p[1]
will give you the next element (second) in that array.
Is the address of an array equal to its first element in C
An array is in fact an extent of memory. So the address of an extent is the address of the stored array in this extent and of its first element.
As a result an array designator used in expressions with rare exceptions is converted to pointer to its first element.
Using the example provided by you
int arr[5] = { 1, 2, 3, 4, 5 };
these expressions &arr
and &arr[0]
have the same value but are of different types.
The expression &arr
has the type int( * )[5]
while the expression &a[0]
has the type int *
.
Here is a demonstrative program.
#include <stdio.h>
int main(void)
{
int arr[5] = { 1, 2, 3, 4, 5 };
printf( "&arr == &arr[0] is %s\n",
( void * )&arr == ( void * )&arr[0] ? "true" : "false" );
printf( "sizeof( *&arr ) = %zu\n", sizeof( *&arr ) );
printf( "sizeof( *&arr[0] ) = %zu\n", sizeof( *&arr[0] ) );
return 0;
}
Its output is
&arr == &arr[0] is true
sizeof( *&arr ) = 20
sizeof( *&arr[0] ) = 4
How store address of an array[] in a variable
It is written:
char (*arrayAddress)[6] = &cha;
Notice that the name of variable gets tucked in middle of the expression.
Address of an array different with the address of the first element?
Why address of array a in func() difference with address of a[0]?
Because you're calling the function func()
passing a
by value. This means the a
inside the function func()
is actually a copy of the original decayed pointer that you're passing to func()
from main()
. And since they're different variables, they have different addresses. Thus by writing cout << &a;
you're printing the address of this separate local variable named a
.
If you want to print the original address, you should write inside func()
:
void func(int a[])
{
//---------------------------------v---------->removed the &
cout << "address in func: " << a << endl;;
cout << "GT: " << &a[0] << endl;
}
Demo
address in main: 0x7ffcfb0a4320
address in main a[0]: 0x7ffcfb0a4320
address in func: 0x7ffcfb0a4320
GT: 0x7ffcfb0a4320
Related Topics
How to Link C++ Program With Boost Using Cmake
Writing Bmp Image in Pure C/C++ Without Other Libraries
Normal Mapping Gone Horribly Wrong
How to Link Opencv in Qtcreator and Use Qt Library
Removing Item from Vector, While in C++11 Range 'For' Loop
Error Lnk2019: Unresolved External Symbol _Winmain@16 Referenced in Function _Tmaincrtstartup
How to Append Text to a Text File in C++
Explicit Specialization in Non-Namespace Scope
Catching Access Violation Exceptions
Is Main() Really Start of a C++ Program
How to Reassign the Reference in C++
Propagating 'Typedef' from Based to Derived Class For 'Template'
Is Stateful Metaprogramming Ill-Formed (Yet)