Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between *ptr and *(&ptr + 1)

Tags:

c

I am new to pointers and learning basics of pointers. I want to know the difference between *ptr and *(&ptr + 1) from the below code.

#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 i.e. 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 in Advance.

like image 301
arahan567 Avatar asked Dec 25 '14 16:12

arahan567


People also ask

What is the difference between * ptr ++ and ++* ptr?

So *ptr++ gets evaluated as *(ptr++) . ptr++ evaluates to the current value of ptr ; it increments ptr only as a side effect. The value of the expression is the same as the current value of ptr .

What is the meaning of * ptr in C?

It has two meanings: // When * is used when declaring a pointer char *ptr means ``pointer to'' a character, e.g., char *ptr = "andrew"; char c; // but when not used in the declaration of a pointer (e.g., *ptr). The indirection operator * // is used to access the data by means of pointer ptr.

What does &* ptr1 mean?

ptr is a pointer and it points to some 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; Else the address of your first element is given just by ptr .

HOW IS &P different from * p?

If p is a pointer (effectively a memory address), then *p is the data that it is pointing to. The & as an operator is the "address of" operator. If p is a bit of data, then &p is a pointer pointing to the data.


3 Answers

&ptr is the address of the pointer variable ptr. This address in unrelated to the address of the array.

*(&ptr + 1) is undefined behavior because &ptr + 1 is one step past the address of ptr. Dereferencing such a pointer produces undefined behavior.

What you probably meant was *(ptr + 1).

like image 85
usr Avatar answered Nov 15 '22 05:11

usr


It seems that the compiler placed defined objects on the stack the following way

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

Thus &ptr is the address where ptr is allocated. &ptr + 1 is the next address after ptr and at the same time it is the address of a[0].

Take into account that it is unspecified in which order the compiler has to place local variables on stack.

Also it seems that in the environment where the program was compiled sizeof( int * ) that corresponds to sizeof( *( &ptr + 1 ) ) is equal to sizeof( int ) and in turn equal to 4. Or sizeof( int * ) is equal to 8 but sizeof( int ) is equal to 4. As you are using format specifier %d in printf for expression *( &ptr + 1 ) then exactly a[0] is outputed.

By the way you can check whether this scheme corresponds to the actual placement of the variables by printing

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

If the addresses will not be equal then it means that there is some arbitrary value at the address &ptr + 1 that does not correspond to a[0].

As for the question in the title of your post

Difference Between *ptr and *(&ptr + 1)

then *ptr corresponds to a[0] while using expression *(&ptr + 1) results in undefined behaviour because you are trying to dereference address after ptr.

like image 44
Vlad from Moscow Avatar answered Nov 15 '22 05:11

Vlad from Moscow


ptr is a pointer and it points to some 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; 

Else the address of your first element is given just by ptr.

If you want to parse through your array then have

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 as this is not the memory which is allocated by you. &ptr is under your control not the location after it i.e. &ptr+1

like image 37
Gopi Avatar answered Nov 15 '22 06:11

Gopi