Chapter10 Pointers
Chapter10 Pointers
cin >> *xp3; // Read value into cell xp3 points to.
// Assume value 7 was the input.
cout << *xp1 << ‘,’ << *xp2 + *xp3; // Produces output: 7,10
NOTE: The 2 “new” cells accessible ONLY via pointers.
4 7 11
starting address of vals: 0x4a00
cout << vals; // displays 0x4a00
cout << vals[0]; // displays 4
• Given:
int vals[]={4,7,11};
int *valptr = vals;
• What is valptr + 1?
• It means (address in valptr) + (1 * size of an int)
cout << *(valptr+1); // displays 7
cout << *(valptr+2); // displays 11
• Must use ( ) in expression
• Array notation
vals[i]
is equivalent to the pointer notation
*(vals + i)
• No bounds checking performed on array
access
Expression Meaning
testPtr->grades Access the grades pointer in
test1. This is the same as
(*testPtr).grades
*testPtr->grades Access the value pointed at by
testPtr->grades. This is the
same as *(*testPtr).grades
*test1.grades Access the value pointed at by
test1.grades