Pointer to an array of n elements of type int (strange addresses)

I wanted to make sure that I understand the concept of a pointer to arrayelements n, for example:int (*myarr)[10]; //myarr is a pointer to an array that can hold 10 integer

I tried the following code:

void main(){
    int arr[][3] = { { 10, 20, 50 }, { 30, 60, 90 }, { 22, 92, 63 } };
        int(*arrptr)[3]; /* a pointer to an array of 3 ints*/
        int *ip = arr[0];
        arrptr = arr;
        int index;
        for (index = 0; index < 9; index++)
        {
            printf("[ %d ][ %p ]\n\n", *ip, ip);ip++;
        }

        for (index = 0; index < 3; index++)
        {
            printf("%x <-> %p,%d\n", *arrptr, arrptr,**arrptr);
            arrptr++;
        }
    }

and I realized that

*[ 10 ][ 001BFA40 ]*

[ 20 ][ 001BFA44 ]

[ 50 ][ 001BFA48 ]

*[ 30 ][ 001BFA4C ]*

[ 60 ][ 001BFA50 ]

[ 90 ][ 001BFA54 ]

*[ 22 ][ 001BFA58 ]*

[ 92 ][ 001BFA5C ]

[ 63 ][ 001BFA60 ]

*1bfa40* <-> *001BFA40*,10
1bfa4c <-> 001BFA4C,30
1bfa58 <-> 001BFA58,22

As I understand it, it arrptrcontains the entire address pointed to by the array. This would mean that the pointer can move freely in these address ranges.

But why is the value of the address that contains arrptrequal to the address that contains the value arr[0]?

In other words: I know that it’s normal that it *arrptrcontains the address arr[0], but the address arrptrthat contains the place where the item arrptrwill point to the same, where arr[0]?

+4
source share
4 answers

- . , ,

&arr, arr, arr[0], &arr[0] &arr[0][0] , . .

, ,

printf("%x <-> %p,%d\n", *arrptr, arrptr,**arrptr);

arrptr - , int ( * )[3];. *arrptr, int[3], , , , int *, arrptr *arrptr . . .:)

, *arrptr int * ( , C , ++ ), **arrptr int. .

+4

*arrptr ( , ) arrptr[0]. arrptr = arr, , *arrptr *arr.

, %x undefined ( %x unsigned). %p. , %p , (void *).

+2

, arrptr, , arr[0]

.

T a[n];  /* n > 0 */

&a == &a[0]

.


, C "2D" - .

+1

%p , :

void * p = 1;

printf("%p, %p\n", p, &p);

- :

0000000000000001, 7fffffff0234585a

- p, - , , p. (As p , ).

0

All Articles