The array name and the array address were the same I made the pointer point to the array and printed out the pointer address, and it came out differently.
Array name = array address
Pointer value! = Pointer address labor Why?
#include <stdio.h>
int main()
{
char my_array[100] = "some cool string";
printf("my_array = %p\n", my_array);
printf("&my_array = %p\n", &my_array);
char *pointer_to_array = my_array;
printf("pointer_to_array = %p\n", pointer_to_array);
printf("&pointer_to_array = %p\n", &pointer_to_array);
printf("Press ENTER to continue...\n");
getchar();
return 0;
}
my_array = 0022FF00
&my_array = 0022FF00
pointer_to_array = 0022FF00
&pointer_to_array = 0022FEFC
Usually, the name of the array is the same as the address of the first element of the array. So the array has the same address and array name.
That doesn't mean that the array address and array name are the same type The results of the two operations are not the same.
When an array is named as an operand for sizeof
or &
(singular operator), the array name becomes the object itself. For example, sizeof = array
.
But if the address of the array is an operand, then it's a type.
For example, if Tarray[size]
then sizeof = T*
.
int main(void) {
int myarr[10] = {1,2,3,4,5,6,7,8,9,10};
//They're the same
printf("Address of array:\t%p\n", &myarr); //Address of array
printf("Name of Array:\t%p\n\n", myarr); //Name of Array
//They're different
printf("array address+1 :\t%p\n", &myarr+1); //array address+1
printf("array name+1 :\t%p\n\n"; myarr+1); //array name+1
//Of course not
printf("**(array address+1):\t%d\n", **(&myarr+1); //**(array address+1) - undefined behavior
printf("*(arrangement name+1) :\t%d\n\n", *(myarr+1)); //*(arrangement name+1) - 2nd value
//Compare sizes
printf("array address size:\t%lu\n", sizeof(&myarr);
printf("int* size:\t%lu\n\n", sizeof(int*));
printf("Arrange name size:\t%lu\n", sizeof(myarr)));
printf("size of int x number of elements 10 : %lu\n", size of(int)*10);
}
Output:
Address of array: 0x7fff5fbff740
Array Name: 0x7fff5fbff740
Array address +1 : 0x7fff5fbff768
Array Name +1 : 0x7fff5fbff744
**(Array address+1): 1389390144
*(Arrange name+1) : 2
Array address size: 8
size of int*: 8
Array Name Size: 40
int size x number of elements 10:40
© 2024 OneMinuteCode. All rights reserved.