Lecture 9

dereferencing ptr returns the value of the object

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: r. • * Dereferencing. *ptr returns the value of the object that ptr points to. 7 Address of Operator & 8 Dereferencing Operator * The address of operator “&” returns the address of an object. The Dereferencing Operator “*” returns the value of an object to which its operand points. Example: &joe returns the address of the variable joe and not the value of it. Example: ptr = &joe assigns the pointer ptr to point to the variable joe. Example: int object = 5; int *ptr; // pointer! ptr = &object; // ptr points to address of object cout << *ptr; // sends 5 to the screen 9 new POINTER INITIALIZATION int *ptr; int x = 5; 10 // declared ptr, value undefined. // declared variable. • A C++ operator that creates a new object of the type that this pointer can point to; • Returns a pointer to that new object. cout << x; // sends 5 to screen. cout << *ptr; // wrong! Pointer not initialized! ptr = &x; // ptr now points to the address of x. cout << *ptr; // right! sends 5 to screen. 11 12...
View Full Document

This document was uploaded on 03/31/2014 for the course CS 114 at NJIT.

Ask a homework question - tutors are online