C Programming - Pointers - Discussion
Discussion Forum : Pointers - Yes / No Questions (Q.No. 4)
4.
Will the program compile in Turbo C?
#include<stdio.h>
int main()
{
int a=10, *j;
void *k;
j=k=&a;
j++;
k++;
printf("%u %u\n", j, k);
return 0;
}
Answer: Option
Explanation:
Error in statement k++. We cannot perform arithmetic on void pointers.
The following error will be displayed while compiling above program in TurboC.
Compiling PROGRAM.C:
Error PROGRAM.C 8: Size of the type is unknown or zero.
Discussion:
17 comments Page 1 of 2.
Siva said:
1 decade ago
1 #include<stdio.h>
2 int main()
3 {
4 int b=0x50,a=0x10203040,c=0x60;
5 void *p=&a,**q;
6 p++;
7 printf("%x\n",*(int *)p);
8 }
Hi friends check this program output in gcc.
It won't get any error and output should be 50102030 or 60102030 based on allocation.
2 int main()
3 {
4 int b=0x50,a=0x10203040,c=0x60;
5 void *p=&a,**q;
6 p++;
7 printf("%x\n",*(int *)p);
8 }
Hi friends check this program output in gcc.
It won't get any error and output should be 50102030 or 60102030 based on allocation.
Akash kumar said:
1 decade ago
There is error because the data type defined of k is void as we know that to use this type of pointer need the typecasting and in the program type casting is not done due to this pointer arithmetic operation cannot be performed.
Manoj said:
1 decade ago
If GCC compiler increments by 1 ,then how it is decided or depends on???
If ++ is used on int pointer then it gets incremented by 4 because int is of size 4 but then what is the size of void???
If ++ is used on int pointer then it gets incremented by 4 because int is of size 4 but then what is the size of void???
Rupinder said:
1 decade ago
If your code output is unexpected and there is no logical and syntactical error and reason for that unexpected output is compiler design,then your code is poorly written.
Andras Joo said:
1 decade ago
In practice some compilers treat void pointers as unsigned char pointers.
void * p = 0;
++p;
printf("%u\n", p);
The output will be 1, if compiled with GCC.
void * p = 0;
++p;
printf("%u\n", p);
The output will be 1, if compiled with GCC.
Devendra said:
9 years ago
Actually, there is confusion with compiler. If you execute it in Turbo C it won't allow arithmetic on VOID* but in GCC it works fine and prints two addresses.
Chandru said:
1 decade ago
Friends, please copy the program and compile with c compiler. It can compile and execute it. The output is garbage value. The answer is wrong.
Pranali said:
8 years ago
@Manoj.
There is no size for void datatype.
Still if we find sizeof(void) it will give 1 byte.(minimum size have to give).
There is no size for void datatype.
Still if we find sizeof(void) it will give 1 byte.(minimum size have to give).
Akhilesh said:
1 decade ago
j=k=&a;
error C2440: '=' : cannot convert from 'void *' to 'int *'.
k++;
error C2036: 'void *' : unknown size.
error C2440: '=' : cannot convert from 'void *' to 'int *'.
k++;
error C2036: 'void *' : unknown size.
Ankit Anand said:
2 decades ago
printf statement should be:-
printf("%u %u\n", *j, *((int *)k));
else everything fine on LINUX
printf("%u %u\n", *j, *((int *)k));
else everything fine on LINUX
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers