Which function increments the integer value pointed to by p?

Enhance your programming skills with the RECF Programming Test. Features flashcards and multiple choice questions with hints and explanations. Prepare for success!

Multiple Choice

Which function increments the integer value pointed to by p?

Explanation:
You're being tested on how to modify the actual value that a pointer refers to. When a function receives a pointer to int, you must access the integer in memory by dereferencing the pointer with the asterisk, and then apply the increment to that value. In the correct function, the pointer parameter is int* p and the statement uses (*p)++: it takes the integer located at p’s address and increases it by one. This changes the real value stored in memory at that location, which is exactly what “increments the integer value pointed to by p” means. Think about the other patterns: incrementing the pointer itself (p++) moves the pointer to another memory address instead of changing the pointed-to value, and doing so inside the function only affects the local copy of the pointer. Incrementing a local copy of the integer (when the parameter isn’t a pointer, like void inc(int p)) also doesn’t alter the original value outside the function. Decrementing the pointed-to value with (*p)-- would reduce the value instead of increasing it.

You're being tested on how to modify the actual value that a pointer refers to. When a function receives a pointer to int, you must access the integer in memory by dereferencing the pointer with the asterisk, and then apply the increment to that value. In the correct function, the pointer parameter is int* p and the statement uses (*p)++: it takes the integer located at p’s address and increases it by one. This changes the real value stored in memory at that location, which is exactly what “increments the integer value pointed to by p” means.

Think about the other patterns: incrementing the pointer itself (p++) moves the pointer to another memory address instead of changing the pointed-to value, and doing so inside the function only affects the local copy of the pointer. Incrementing a local copy of the integer (when the parameter isn’t a pointer, like void inc(int p)) also doesn’t alter the original value outside the function. Decrementing the pointed-to value with (*p)-- would reduce the value instead of increasing it.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy