Please explain the difference.

I have a program about two-dimensional arrays

base address 8678

#include<stdio.h>
#include<conio.h>
main()
{
 int arr[3][3]={
                 {83,8,43},
                 {73,45,6},
                 {34,67,9}
                 };
printf("%d ",&arr+1);  //points to 8696
printf("%d ",arr+1);   //points to 8684
return 0;
}  

what's the difference between arr+1and &arr+1?

+5
source share
4 answers

Well, these are different things. arrsplits in most contexts into a pointer to the first element of your array - this means a pointer to the first 3-element row in your 2D array: enter int (*)[3]. arr + 1, then, points to the second line in the array.

&arris the address of the array itself (type int (*)[3][3]), so it &arr + 1points to memory just beyond the end of your entire 2D array.

, -. , , , . :

printf("%ld\n",(intptr_t)(&arr+1) - (intptr_t)arr);
printf("%ld\n",(intptr_t)(arr+1) - (intptr_t)arr);

&arr+1 arr+1 . , :

36
12

36 : 3 ร— 3 ร— 4 = 36 . 12: 1 ร— 3 ร— 4 = 12 .

. , %d, . , %p .

+9

: X [Y] === * (X + Y)

* (arr + 1) === arr [1], arr + 1 === & arr [1]

, & arr + 1 === & ((& arr) [1])

(& arr) [1]? , (& arr) [0] === * & arr === arr, .. 3x3, (& arr) [1] 3x3, & arr + 1 === & ((& arr) [1]) 3x3, & arr... .

+1

Arr + 1 gives the next element in the array, while & arr +1 gives the address of the next array of integers

0
source

array + 1 means the address of array [1], and it costs 3 int memory.

& array + 1 means the address of the array [0] add 1;

-1
source

All Articles