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

Discussion

Consider the following C program.

void f(int, short);
void main()
{
     int i = 100;
     short s = 12;
     short *p = &s;
     __________ ;    // call to f()
}

Which one of the following expressions, when placed in the blank above, will NOT result in a type checking error?

  • A.f(s,*s)
  • B.i = f(i,s)
  • C.f(i,*s)
  • D.f(i,*p)

Answer: D

The call that type-checks is f(i,*p).

f is declared as taking an int and a short and returning void. \(i\) is an int, and p is a pointer to short, so *p is a short: the arguments match exactly.

The other choices fail. f(s,*s) and f(i,*s) dereference s, which is a short, not a pointer, so *s is a type error. i = f(i,s) has valid arguments but assigns the result of a void function to an int, which is also a type error.

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

Post your comment

Please log in or register to post a comment.