C Programming - Pointers - Discussion

Discussion Forum : Pointers - Yes / No Questions (Q.No. 5)
5.
Will the following program give any warning on compilation in TurboC (under DOS)?
#include<stdio.h>

int main()
{
    int *p1, i=25;
    void *p2;
    p1=&i;
    p2=&i;
    p1=p2;
    p2=p1;
    return 0;
}
Yes
No
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
21 comments Page 1 of 3.

Anshu said:   1 decade ago
This program will not give any error or warning on gcc compiler until and unless we will not try to print p2 value.

But if we try to print p2 value it will give an error such as:

Warning: dereferencing 'void *' pointer
Error: invalid use of void expression.

So solve this this kind of error,we need to typecast the void pointer.
Such as:

printf("value of p2 is %d
",*(int*)p2);

Thank you.
(4)

Sundar said:   1 decade ago
In Turbo C there is no warning or error on compilation.

But in GCC, it shows the following error:

Error: invalid conversion from 'void*' to 'int*'.

This is due to platform dependency. GCC is a modern compiler while comparing to the typical Turbo C.
(1)

Lohitha said:   1 decade ago
Here we've declared 'p2' as the void pointer, which meaning it can refer to any kind of data type.

we've refered it to an integer pointer as well as the referance of integer variable to pointing to the void pointer.

Is program is legal.

Datta said:   1 decade ago
Turbo c compiler can run this program because there is not check this program,but in borland c or Visual studio will have error.we can not use 2nd time void pointer without typecast.

Sonal said:   1 decade ago
No @badusa, its not like that, void means anything; it can store any data of any type.

Compiler can only follow the instruction & there is no warning.

Mohanty said:   1 decade ago
Is void valid only for pointer value?

Because if I change it to a normal variable it shows-.

Error: "variable or field declared void".

Tanvi said:   1 decade ago
It is compiling on Turbo C. Ideally it should not as yes, arithmetic cannot be performed on Void Pointer.
(1)

Badhusa said:   1 decade ago
Here we defined main function type as int. Then how can we declare void within this main function?

Satyam said:   1 decade ago
Should be converted like for integer (*((int *) voidpointervariable)

DjRahul said:   1 decade ago
But we didn't typecast it and still we are using it how?


Post your comments here:

Your comments will be displayed after verification.