Home » C Programming » Structures » Question
  1. What will be the output of the following C code?
    #include <stdio.h>
    struct p
    {
    int x;
    char y;
    };
    void foo(struct p* );
    int main()
    {
    typedef struct p* q;
    struct p p1[] = {15, 52, 43, 34, 25, 16};
    foo(p1);
    }
    void foo(struct p* p1)
    {
    q ptr1 = p1;
    printf("%d\n", ptr1->x);
    }
    1. Undefined behaviour
    2. 15
    3. Compilation Error
    4. Segmentation fault
    5. None of these
Correct Option: C

Compilation Error

main.c: In function ‘foo’:
main.c:16:9: error: unknown type name ‘q’
q ptr1 = p1;
^
main.c:16:18: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
q ptr1 = p1;
^~
main.c:17:28: error: invalid type argument of ‘->’ (have ‘int’)
printf("%d\n", ptr1->x);



Your comments will be displayed only after manual approval.