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;
}
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.
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.
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)
Divya said:
9 years ago
It's impossible.
(1)
Kariyappa said:
1 decade ago
No error, because void pointer can take any datatype.
(1)
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)
Sangee said:
1 decade ago
Error :invalid conversion from void* to int *.
Mohammad Shabaz said:
4 years ago
Why can't we use Arithmetic on void pointers?
Srisailam said:
8 years ago
In GCC compiler showing no error and no output.
Raj said:
9 years ago
Please explain it.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers