Machine-translated by Google. Questions are authored in English.
Top

Discussion

Point out the compile time error in the program given below.

#include <stdio.h>

int main()
{
    int *x;
    *x=100;
    return 0;
}

 

  • A.Error: invalid assignment for x
  • B.Error: suspicious pointer conversion
  • C.No error
  • D.None of above

Answer: C

Correct answer: No error

The compiler has nothing to object to: x is a pointer to int and 100 is an int, so *x = 100 is well typed. The fault is at run time, not compile time - x was never given a value, so the assignment writes through an indeterminate address. This is the distinction the question is testing: a program can be perfectly legal and still crash.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.