C Programming - Pointers - Discussion

3. 

The following program reports an error on compilation.

#include<stdio.h>
int main()
{
    float i=10, *j;
    void *k;
    k=&i;
    j=k;
    printf("%f\n", *j);
    return 0;
}

[A]. True
[B]. False

Answer: Option B

Explanation:

This program will NOT report any error. (Tested in Turbo C under DOS and GCC under Linux)

The output: 10.000000

Pruthvi said: (Sep 17, 2011)  
In turbo c it is an error. And it is not the correct way to assign a void pointer.

Vivek said: (Feb 27, 2012)  
This is right program. No error will be there.

Kranthi said: (Apr 9, 2012)  
No, Its an error. How come you declare a variable with Void prototype. Void means nothing.

Kunal Bansal said: (Aug 19, 2012)  
I have personally run this program on DEV C++ 4.9.9.2 and it was giving error in line j=k; statement
After type casting it , it is giving the value 10.000000
So this is compiler dependent

Kunal Bansal said: (Aug 19, 2012)  
@kranthi this is an valid prototype
error is because of different thing , read my previous comment

Ankit said: (Jun 14, 2013)  
But before assigning the void pointer we must typecast it. Right ?

Kiran Ware said: (Jan 11, 2014)  
Yes, before assigning the void pointer we must typecast it.

Shabana said: (Apr 10, 2014)  
Yes its correct because void is nothing we cannot assign like that.

Abi said: (May 26, 2014)  
Hi,

Can you explain then why do we need to typecast a void pointer?

Harshu Smart Boy said: (Jun 10, 2014)  
What is possible error?

Suki said: (Jul 11, 2014)  
Can you explain what is typecast?

Rahul said: (Feb 5, 2015)  
The above code is correct. It gave no error during compilation or during running in gcc.

Tripti said: (Mar 14, 2015)  
It produce an error b/c j does not define as a double pointer and we send in it an address of address.

Vidhiyasri said: (Mar 19, 2015)  
It is very useful and I will try my best.

Patel said: (Apr 11, 2015)  
What is meaning of void *k.

Anjaneyareddy said: (Jul 14, 2015)  
Everything is ok. But 1 thing I can't understood. After printing float value 10.000000. Why printed 6 0's?

Chipku said: (Jul 31, 2015)  
Can k=&i possible?

Suman said: (Sep 6, 2015)  
This program gives error.

Error 1: error C2440: '=' : cannot convert from 'void *' to 'float *'.

2 IntelliSense: A value of type "void *" cannot be assigned to an entity of type "float *".

Sboy said: (Mar 20, 2016)  
Invalid conversion compile time error in codeblocks windows.

Siya said: (May 12, 2016)  
What is meant by typecasting?

Mohammad said: (Jul 21, 2017)  
In code blocks on windows 7.

Error: invalid conversion from 'void*' to 'float*' [-fpermissive].

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.