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;
}
True
False
Answer: Option
Explanation:
This program will NOT report any error. (Tested in Turbo C under DOS and GCC under Linux)

The output: 10.000000
Discussion:
23 comments Page 3 of 3.

Mohammad said:   8 years ago
In code blocks on windows 7.

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

Rashmi said:   5 years ago
Before assigning a void pointer => void *k, typecasting has to be done => (int*)k or *((int*)k).

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.


Post your comments here:

Your comments will be displayed after verification.