OA. free
Free
Qualcomm Embedded Systems & Hardware Embedded Systems & Hardware Medium

Point out the error(s) if any in the program given below.

Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.

Point out the error(s) if any in the program given below.

#include <stdio.h>

int main()
{
    struct column
    {
        int c;
        char m;
    };
    
    int co = {3, 'c'};     //Line 1
    char *m;
    
    m = (int *) &co;       //Line 2
    *m = 30;
    
    return 0;
}
Choose one option.
Show answer & explanation
Answer: D. Both Line 1 and Line 2

Line 1 has a type mismatch: co is declared as int but initialized with a struct initializer {3, 'c'}, which is invalid. Line 2 has an incorrect cast: &co is an int* (address of an int), but it's being cast to and assigned to a char*, causing type mismatch and pointer arithmetic issues. Both lines contain compile-time errors.

Step-by-step Derivation:
Analysis of errors:

Line 1 Error:

  • Declaration: int co = {3, 'c'};
  • co is declared as type int, but the initializer {3, 'c'} is a struct initializer (two elements: an int and a char).
  • This is a type mismatch. The struct initializer cannot be used to initialize a scalar int variable.
  • Correct form should be: struct column co = {3, 'c'};

Line 2 Error:

  • Statement: m = (int *) &co;
  • Even if Line 1 were fixed and co were a struct column, &co would have type struct column*.
  • Casting &co to int* and assigning to char* m creates a type mismatch.
  • The cast should be: m = (char *) &co; (if the intent is to access the struct via a char pointer).
  • Additionally, dereferencing with *m = 30; would interpret the memory at &co as a char, which may not align with the struct layout intended.

Conclusion: Both Line 1 and Line 2 contain errors.