It's expected that the code should display "AAA". Select lines that contain errors.
#include <stdio.h>

int main() {
  char *pc = static_cast<char*>(operator new char[3]);  // 1

  for (int i = 0; i < 3; ++i)
    pc[i] = char("A");  // 2

  for (int i = 0; i < 3; ++i)
   printf("%c", pc[i]);  // 3

  delete pc;  // 4
In the line // 1 operator new[] should be used, to which the memory size needed for an array should be passed. Since sizeof(char) == 1, in this case, it's not necessary to multiply the number of elements in the array by sizeof(char).

In the line // 2, character literal should be used instead of string literal.

In the line // 4 operator delete[] should be used, since memory for an array was allocated by the operator new[].

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Sign Up Now
or Subscribe for future quizzes