Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

C | Pointer Basics | Question 14

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Predict the output of following program




#include<stdio.h>
int main()
{
    int a = 12;
    void *ptr = (int *)&a;
    printf("%d", *ptr);
    getchar();
    return 0;
}

(A) 12
(B) Compiler Error
(C) Runt Time Error
(D) 0


Answer: (B)

Explanation: There is compiler error in line “printf(“%d”, *ptr);”.

void * type pointers cannot be de-referenced. We must type cast them before de-referencing.

The following program works fine and prints 12.

#include<stdio.h>

int main()
{
    int a = 12;
    void *ptr = (int *)&a;
    printf("%d", *(int *)ptr);
    getchar();
    return 0;
}


Quiz of this Question

My Personal Notes arrow_drop_up
Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads