1

The following code shows a pointer pointing to first element of array. The ptr shows the address it is pointing to whereas *ptr is printing the value at the address, which is expected.

int arr[]={1,2};
int *ptr=arr; //same as int *ptr=&arr[0]
cout<<"\n\n";
cout<<"Value at "<<ptr<<"is "<<*ptr<<endl;

Output:

Value at 003CFB48is 1

In the following code the pointer is pointing to an array of 2 elements(instead of particularly pointing to an element of an array). But in this case dereferencing the pointer prints the address. ptr2 prints the address then why doesnt *ptr2 print the value at the address?

int arr2[]={3,4};
int (*ptr2)[2]=&arr2;
cout<<"Value at "<<ptr2<<"is "<<*ptr2<<endl; //Isnt *ptr2 supposed to print value at ptr?
cout<<"1st element is "<<**ptr2<<" and second is "<<*(*ptr2+1);

Output:

Value at 003CFB2Cis 003CFB2C
1st element is 3 and second is 4

Edit: It would have been fine if ptr prints an address, *ptr prints some value,**ptr prints the first element(except i would have posted a different question then :) ) I understand that ptr2 here points to an array and not the first element so i might be required to use **ptr2 to print the first element. The question here really is that if ptr2 is pointing to some address then syntatically why would not *ptr2 print the value at that address.

1
  • 2
    There are (more or less subtle) differences between a pointer to the first element and the array. However what you're doing in the second example is taking the "address" of arr2 which means a pointer to the array (which acts somewhat like a pointer to a pointer). So why shouldn't you expect that behaviour? Commented Aug 18, 2015 at 12:58

3 Answers 3

4

Although the numeric value of the two pointers is the same, the difference in their type makes dereference return different things:

  • Dereferencing a pointer to int returns an int, while
  • Dereferencing a pointer to an int array of two elements returns an int array of two elements.

When you pass operator << the dereferenced pointer to an array, it gets a pointer to the first element of that array. Numerically, it's the same pointer, but it is still a pointer. That is why 003CFB2C gets printed.

Note that the expression *(*ptr2+1) does not add much value, because * has higher precedence than +. 1 sis added to a dereferenced ptr2, which behaves like a pointer to int.

Trying to print *(*(ptr2+1)), however, would be undefined behavior, because it would point past the end of arr2. Here is a way to demonstrate what happens when you add 1 to a pointer to an array:

int arr2[2][2]={{3,4},{5,6}};
int (*ptr2)[2]=&arr2[0];
cout<<"Value at "<<ptr2<<"is "<<*ptr2<<endl; //Isnt *ptr2 supposed to print value at ptr?
cout<<"1st element is "<<**ptr2<<" and second is "<<*(*(ptr2+1));

Now 1st element is 3 and second is 5 gets printed (demo), because ptr2+1 points to the next array of two ints, i.e. {5, 6}, its first derefnerence produces the array itself, and its second dereference produces the value at the initial location.

Sign up to request clarification or add additional context in comments.

4 Comments

@dashblinkenlight : the expression *(*ptr2+1) does not add much value, because * has higher precedence than +. That's why 4 that gets printed is simply 3+1, not the 4 from the location that follows seems not to be true. you can try the same example from the question and the second element as 7(or anything other than 4). Even though * has higher precedence than +, *ptr2 at this moment still points to the memory location and hence adding 1 points to the next memory cell i.e. 7.
@anurag86 You are right. I edited this part to explain what's going on.
Would the behavior be undefined? Or ptr+x would be in the multiples of the size of array. Example : If the pointer to array is of size 3, then ptr+3 would jump 6 locations. Wouldn't it always be the case and hence not UB?
@anurag86 The + operation would cause no UB, but the * on the result would. ptr2+x moves in increments of sizeof, but in this case that's the sizeof(int[2]). If ptr2 points to arr in your example, ptr2+1 would be past the end of arr, so dereferencing it would be UB. That is why I added another dimension to the array, so that adding 1 to my ptr2 would land in a valid location.
2

int (*ptr2)[2]=&arr2; This is a pointer to an array.

ptr2 is a pointer to an array. When you print it, you get the address pointed at (the address of the array).

*ptr2 is the array itself. Whenever an array is used in an expression, it decays into a pointer to the first element. When you print the pointer to the first element, you get the address of the first element. Which is also the address of the array.

**ptr2 First you dereference the array pointer and get an array. The array decays into a pointer to the first element. Then you take the contents of the pointer to the first element and therefore the first element is printed.

1 Comment

Thanks a lot for the explanation.
0

*ptr2 is printing the address of arr ,ptr2 is a pointer to an array.

2 Comments

Yes :( Should i delete my answer?
That's up to you, although the down votes seem to be disappearing now that you have fixed the post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.