Want to understand C array behavior

See the following code snippet:

int main()
{

    int arr[] = { 0,3 , 4,28,1198};
    for(int i=0;i<5;i++)
    printf("\n arr[i] %u \n" , arr+i);
    printf("\n *******************\n");
    printf("%u ", &arr+1);

    return 1;

}

When it starts, it issues:

arr[i] 3219650892 

 arr[i] 3219650896 

 arr[i] 3219650900 

 arr[i] 3219650904 

 arr[i] 3219650908 

 *******************
3219650912 

It seems to be showing me the last element address added with another integer, which seems weird. I feel that this should have given me the address of the second element.

Can you help me understand this behavior?

+5
source share
3 answers

To understand this, compare the value arrwith &arr.

arr - . C , ( , ). , arr &arr[0], . int, , 1 , int. , .

, &arr . * , . &arr - " int". 1 , int. , int.

, %u. %p void *, :

printf("%p ", (void *) (&arr+1));

* : &, . &arr, arr - , , &arr - .

+10

&arr+1

&arr arr, &arr + 1 arr array + 1. arr &arr[4], &arr + 1 - , &arr[5] ( ).

+3

& arr gives you a pointer to an array of 5 integers.

therefore, & arr + 1 points to the next array of 5 integers.

0
source

All Articles