C Programming - Pointers - Discussion
Discussion Forum : Pointers - True / False Questions (Q.No. 3)
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;
}
Answer: Option
Explanation:
This program will NOT report any error. (Tested in Turbo C under DOS and GCC under Linux)
The output: 10.000000
The output: 10.000000
Discussion:
23 comments Page 1 of 3.
KRISH BHANDERI said:
2 years ago
@All.
Here is my code;
#include<stdio.h>
int main()
{
float i=10, *j;
void *k;
k=&i;
j=k;
printf("%f\n", *j);
return 0;
}
in the Above program, void pointer holds the Address of Float i
Remember Whenever we use a void pointer we have to typecast it to print the value
printf("%d",*(float*)k);
But here what we done is we assigned the address of i which is hold by k
j=k
now j holds the address of i
So, it will print 10.000000.
Here is my code;
#include<stdio.h>
int main()
{
float i=10, *j;
void *k;
k=&i;
j=k;
printf("%f\n", *j);
return 0;
}
in the Above program, void pointer holds the Address of Float i
Remember Whenever we use a void pointer we have to typecast it to print the value
printf("%d",*(float*)k);
But here what we done is we assigned the address of i which is hold by k
j=k
now j holds the address of i
So, it will print 10.000000.
Suman said:
10 years ago
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 *".
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 *".
Kunal Bansal said:
1 decade ago
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
After type casting it , it is giving the value 10.000000
So this is compiler dependent
Anjaneyareddy said:
1 decade ago
Everything is ok. But 1 thing I can't understood. After printing float value 10.000000. Why printed 6 0's?
Rashmi said:
5 years ago
Before assigning a void pointer => void *k, typecasting has to be done => (int*)k or *((int*)k).
Tripti said:
1 decade ago
It produce an error b/c j does not define as a double pointer and we send in it an address of address.
Kunal Bansal said:
1 decade ago
@kranthi this is an valid prototype
error is because of different thing , read my previous comment
error is because of different thing , read my previous comment
Mohammad said:
8 years ago
In code blocks on windows 7.
Error: invalid conversion from 'void*' to 'float*' [-fpermissive].
Error: invalid conversion from 'void*' to 'float*' [-fpermissive].
Kranthi said:
1 decade ago
No, Its an error. How come you declare a variable with Void prototype. Void means nothing.
Rahul said:
1 decade ago
The above code is correct. It gave no error during compilation or during running in gcc.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers