#include <stdio.h>
int main()
{
int arr[2][4];
for (int i = 0; i < sizeof(arr)/sizeof(int); i++) {
scanf("%d", &arr[0][0] + i); // ok
// // scanf("%d", &arr + i); // error
}
printf("arr size : %d\n", sizeof(arr)/ sizeof(int));
for (int i = 0; i < sizeof(arr) / sizeof(int); i++) {
printf("%d ", arr[0][i]);
}
return 0;
}
When storing values in an array, &arr[0][0] +i and &arr + i have different results. I thought it'd be like my first address. I wonder how it's different.
c
If the result is the same as &arr[0][0]+i
, it is arr[0]+i
right? &arr[0][0]+i
and &arr+i
are different, I will write down below.
&arr[0][0]+i
is the int *
type, with operators applied in the order (&(arr[0][0]))+i
.
&arr+i
is int(*2)[2][4]
because the operators are applied in the order (&arr)+i
.
#include <stdio.h>
int main()
{
int arr[2][4] = {{1,2,3,4},{5,6,7,8}};
printf("%d\n", &arr[0][0]);
printf("%d\n", &arr[0][0] + 1);
printf("%d\n", &arr[0]] + 1);
printf("%d\n", &arr);
printf("%d\n", &arr + 1);
return 0;
}
© 2024 OneMinuteCode. All rights reserved.