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.
arr2which means a pointer to the array (which acts somewhat like a pointer to a pointer). So why shouldn't you expect that behaviour?