Which of the line(s) generates compilation error(s) in the program given below?
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Which of the line(s) generates compilation error(s) in the program given below?
#include <stdio.h>
int main(int *p) // Line 1
{
void *vtr;
int ab = 22;
vtr = &ab;
p = vtr; // Line 2
printf("%d", ++*p + ++*vtr); // Line 3
return 0;
}
Show answer & explanation
Line 1 is a compilation error because main() must have return type int and parameter list (void) or (int argc, char *argv[]), not (int *p). Line 3 is a compilation error because void pointers cannot be dereferenced (vtr) or incremented (++vtr) without explicit casting—void pointer arithmetic is not allowed in C. Line 2 technically compiles (implicit conversion from void to int) but is unsafe.
Step-by-step Derivation:
Analysis of each line:
Line 1: int main(int *p) — COMPILATION ERROR. The main() function signature must be either int main(void) or int main(int argc, char *argv[]). Having a parameter int *p is non-standard and causes a compilation error.
Line 2: p = vtr; — COMPILES (but is unsafe). Assigning void* to int* is allowed; the compiler performs implicit conversion without error.
Line 3: printf("%d", ++*p + ++*vtr); — COMPILATION ERROR. The expression ++*vtr attempts to:
- Dereference a void pointer (*vtr) — not allowed without casting
- Increment the dereferenced value (++*vtr) — compounds the error
Void pointers are generic pointers used for type-agnostic storage. They cannot be dereferenced or used in arithmetic without an explicit cast to a concrete type (e.g., (int*)vtr). This is a language constraint to prevent undefined pointer arithmetic.
Result: Lines 1 and 3 both generate compilation errors.