The difference between * ptr and * (& ptr + 1)

I am new to pointers and pointer training principles. I want to know the difference between * ptr and * (& ptr + 1) from the code below.

#include<stdio.h> int main() { int a[3] = {1, 2, 3}; int *ptr = a; printf("*ptr = %d\n",*ptr); printf("&ptr = %p\n",&ptr); printf("(&ptr + 1) = %p\n",(&ptr + 1)); printf("*(&ptr + 1) = %d\n",*(&ptr + 1)); return 0; } 

From my analysis, gcc produced the following output:

 *ptr = 1 // as ptr = a, Ultimately *ptr will print the first value of array ie 1 &ptr = 0x7fffa7e97788 // Starting Address of array or address of first element (&ptr + 1) = 0x7fffa7e97790 //address of next element in the array *(&ptr + 1) = 1 // I want to know how this is getting dereffered 

Thanks at Advance.

+5
source share
4 answers

&ptr is the address of the ptr pointer variable. This address is not associated with the address of the array.

*(&ptr + 1) is undefined because &ptr + 1 is one step below the ptr address. Highlighting such a pointer causes undefined behavior.

Perhaps you meant *(ptr + 1) .

+8
source

It seems that the compiler pushed certain objects on the stack as follows

 -------------------------------- | ptr | a[0] | a[1] | a[2] | -------------------------------- 

So &ptr is the address where ptr is distributed. &ptr + 1 is the next address after ptr and at the same time it is address [0].

Note that it is unspecified in what order the compiler should push local variables onto the stack.

It also seems that in the environment where the sizeof( int * ) program was compiled, which corresponds to sizeof( *( &ptr + 1 ) ) , it is equal to sizeof( int ) and, in turn, is 4. Or sizeof( int * ) is 8, but sizeof( int ) is 4. Because you use the %d format specifier in printf to express *( &ptr + 1 ) , then exactly [0] is output.

By the way, you can check if this scheme matches the actual placement of variables by printing

 printf("(&ptr + 1) = %p\n", &ptr + 1 ); printf("(&a[0]) = %p\n", &a[0] ); 

If the addresses are not equal, then this means that there is some arbitrary value at the address &ptr + 1 , which does not correspond to [0].

Regarding the question in the header of your message

The difference between * ptr and * (& ptr + 1)

then *ptr matches a[0] , while the expression *(&ptr + 1) leads to undefined behavior because you are trying to dereference the address after ptr.

+6
source

ptr is a pointer that points to a valid memory location in your case.

&ptr is the address of your pointer, and it can be assigned to a double pointer

 int **ptr1 = &ptr; 

Otherwise, the address of your first element is indicated only by ptr .

If you want to parse an array, then

 for(i=0;i<4;i++) { printf("Value %d is stored in %p\n",ptr[i],(void *)(ptr+i)); } 

(&ptr+1) being dereferenced leads to undefined behavior, since it is not the memory allocated by you. &ptr is under your control, not the location after it, i.e. &ptr+1

+1
source

Well, that seems interesting. First, since you know that *ptr gives the value a[0] , ptr contains the address a[0] and &ptr is the address of the variable ptr ie integer.

With this in mind, when printing *ptr , the value a[0] ie 1 will be printed, (&ptr+1) will be addressed one step after the address ptr and *(&ptr+1) will print the value at the address (&ptr+1) . In the above code, the address a[0] is located immediately after the address ptr (ie &ptr ). Therefore, when you type *(&ptr+1) , it prints 1 , because &ptr+1 points to a[0] . It depends on the memory allocated to the variables.

Declare the above variables global, and *(&ptr+1) will print a value of zero (if '* (& ptr + 1)' does not indicate any location, because global variables are initialized to zero). Also note the difference in address assigned to variables when declaring global and local. Hope this helps.

0
source

Source: https://habr.com/ru/post/1209886/


All Articles