213k views
0 votes
The following statement:

int *ptr = new int;

A) results in a compiler error
B) assigns an integer less than 32767 to the variable named ptr
C) assigns an address to the variable named ptr
D) creates a new pointer named int
E) None of these

1 Answer

4 votes

Final answer:

C) assigns an address to the variable named ptr. The statement 'int *ptr = new int;' assigns an address to the variable named ptr and dynamically allocates memory for an integer on the heap.

Step-by-step explanation:

The statement int *ptr = new int; assigns an address to the variable named ptr. It does not result in a compiler error and it does not assign a specific integer value to the variable. Instead, it dynamically allocates memory for an integer on the heap and stores the address of that memory location in the variable ptr.

Here's a step-by-step explanation:

  1. The keyword new is used to dynamically allocate memory on the heap.
  2. int specifies the data type of the memory block we want to allocate, which in this case is an integer.
  3. The result of the new int; expression is an address to the allocated memory block.
  4. The * indicates that ptr is a pointer variable, which means it stores memory addresses instead of actual values.
  5. ptr is assigned the address of the newly allocated memory block.

So, the correct answer is C) assigns an address to the variable named ptr.

User Jay Smith
by
7.7k points