value of this line of code

Hello everyone. I was wondering if this is correct:

*(arr_ptr+4) = arr_ptr+4

Is arr_ptr+4 considered the value?

Thanks,

Mary
Assuming arr_ptr is a pointer to an array. The dereferencing operator * in *(arr_ptr+4) gets the value stored in the object that the pointer refers to, which would be the fifth element of the array.

A pointer stores a memory address - The value of arr_ptr+4 by itself is the memory address of the object it points to.


1
2
3
4
5
6
7
8
9
10
11
12
13
        int numarray[] = {0,1,2,3,4,5};
	int * arrayPtr = numarray;

	
	cout << numarray[4] << endl;
	cout << arrayPtr[4] << endl;
	cout << *(numarray+4) << endl;
	cout << *(arrayPtr+4) << endl;

	cout << &numarray[4] << endl;
	cout << &arrayPtr[4] << endl;
	cout << numarray+4 << endl;
	cout << arrayPtr+4 << endl;		


Output

4
4
4
4

0xbf945c68
0xbf945c68
0xbf945c68
0xbf945c68
Last edited on
Topic archived. No new replies allowed.