Computer Sciences > Gate 2016 Set-1 > Functions
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)

Correct : F

Given:
Function prototype: void f(int, short);
Variables declared: int i = 100; short s = 12; short *p = &s;
To find:
The expression that will NOT cause a type checking error when placed in the blank.
Solution:
Let me look at the function declaration first.
The function f takes two arguments: the first of type int, and the second of type short.
It returns void, so it cannot be assigned to any variable.
Now let me check the given variables:
i is of type int.
s is of type short.
p is of type pointer to short (short *).
Let me test each option one by one:
Checking Option A: f(s, *s)
Here, s is an integer type (short), not a pointer. Applying the indirection operator * on s (*s) is invalid and causes a type error.
Checking Option B: i = f(i, s)
The function f returns void. We cannot assign a void expression to an int variable i. This will cause a type error.
Checking Option C: f(i, *s)
Again, s is not a pointer, so *s is an invalid operation.
Checking Option D: f(i, *p)
Here, i is of type int, which matches the first parameter.
p is of type short*, so dereferencing it as *p gives a value of type short, which matches the second parameter.
The function call does not attempt to assign the void return value to anything.
So, this call is completely valid and does not produce any type checking error.
Answer: (D)

Similar Questions

The following function computes the maximum value contained in an integer array p[] of size n (n >= 1) int max(int *p, int n) {     int a=0, b=n...
#123 MCQ
Consider the following function implemented in C: void printxy(int x, int y) {     int *ptr;     x = 0;     ptr = &x;...
#159 MCQ
Consider the following program: int f(int *p, int n) {     if (n
#545 Fill in the Blanks

Related Topics

C program type checking error function call C C pointers GATE Computer Sciences 2016 GATE CS 2016 Set-1 Q26 pointer dereferencing C

Unique Visitor Count

Total Unique Visitors

Loading......