< Previous | Contents | Next >

Questions and Answers

Q: How is a pointer different from the variable to which it points?

A: A pointer stores a memory address. If a pointer points to a variable, it stores the address of that variable.

Q: What good is it to store the address of a variable that already exists?

A: One big advantage of storing the address of an existing variable is that you can pass a pointer to the variable for efficiency instead of passing the variable by value.

Q: Does a pointer always have to point to an existing variable?

A: No. You can create a pointer that points to an unnamed chunk of computer memory as you need it. Youll learn more about allocating memory in this dynamic fashion in Chapter 9, Advanced Classes and Dynamic Memory: Game Lobby.

Q: Why should I pass variables using references instead of pointers whenever possible?

A: Because of the sweet, syntactic sugar that references provide. Passing a reference or a pointer is an efficient way to provide access to objects, but pointers require extra syntax (like the dereference operator) to access the object itself.

Q: Why should I initialize a pointer when I declare it or soon thereafter?

A: Because dereferencing an uninitialized pointer can lead to disastrous results, including a program crash.

Questions and Answers 251


Q: Whats a dangling pointer?

A: A pointer that points to an invalid memory location, where any data could exist.

Q: Whats so dangerous about a dangling pointer?

A: Like using an uninitialized pointer, using a dangling pointer can lead to disastrous results, including a program crash.

Q: Why should I initialize a pointer to 0?

A: By initializing a pointer to 0, you create a null pointer, which is understood as a pointer to nothing.

Q: So then its safe to dereference a null pointer, right?

A: No! Although its good programming practice to assign 0 to a pointer that doesnt point to an object, dereferencing a null pointer is as dangerous as dereferencing a dangling pointer.

Q: What will happen if I dereference a null pointer?

A: Just like dereferencing a dangling pointer or an uninitialized pointer, the results are unpredictable. Most likely, youll crash your program.

Q: What good are null pointers?

A: Theyre often returned by functions as a sign of failure. For example, if a function is supposed to return a pointer to an object that represents the graphics screen, but that function couldnt initialize the screen, it might return a null pointer.

Q: How does using the keyword const when declaring a pointer affect the pointer?

A: It depends on how you use it. Generally, you use const when you are declaring a pointer to restrict what the pointer can do.

Q: What kinds of restrictions can I impose on a pointer by declaring it with

const?

A: You can restrict a pointer so it can only point to the object it was initialized to point to, or you can restrict a pointer so it cant change the value of the object it points to, or both.

252 Chapter 7 n Pointers: Tic-Tac-Toe 2.0


Q: Why would I want to restrict what a pointer can do?

A: For safety. For example, you might be working with an object that you know you dont want to change.

Q: To what type of pointers can I assign a constant value?

A: A pointer to a constant or a constant pointer to a constant. Q: How can I safely return a pointer from a function?

A: One way is by returning a pointer to an object that you received from the calling function. This way, youre returning a pointer to an object that exists back in the calling code. (In Chapter 9, youll discover another important way when you learn about dynamic memory.)