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.

Mohammad Shabaz said:   3 years ago
Why can't we use Arithmetic on void pointers?

Srisailam said:   7 years ago
In GCC compiler showing no error and no output.

Raj said:   7 years ago
Please explain it.

Aarzu said:   8 years ago
What is use of void pointer?

Divya said:   8 years ago
It's impossible.
(1)

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

Kariyappa said:   1 decade ago
No error, because void pointer can take any datatype.

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)

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)

Diksh said:   1 decade ago
I agreed with sonal.
(1)


Post your comments here:

Your comments will be displayed after verification.