C Programming - Pointers - Discussion

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;
}

[A]. Yes
[B]. No

Answer: Option B

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.


Ankit Anand said: (Jun 25, 2010)  
printf statement should be:-

printf("%u %u\n", *j, *((int *)k));

else everything fine on LINUX

Andras Joo said: (Oct 1, 2010)  
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.

Manoj said: (Sep 9, 2011)  
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???

Rupinder said: (Nov 30, 2011)  
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.

Megala said: (Dec 23, 2011)  
I think the statement
j=k=&a;
is wrong

It will generate compiler error.

Sam said: (Jan 20, 2012)  
It will be compiled. As it should display the memory locations next to the location of a.

Manish said: (Feb 18, 2012)  
The void pointer can't perform arithmetic operation.

Vishu said: (Apr 24, 2012)  
There are no errors in the above program. I compiled and I got the output.

Akash Kumar said: (Aug 27, 2013)  
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.

Akhilesh said: (Sep 2, 2013)  
j=k=&a;
error C2440: '=' : cannot convert from 'void *' to 'int *'.

k++;
error C2036: 'void *' : unknown size.

Chandru said: (Sep 14, 2013)  
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.

Siva said: (Sep 23, 2013)  
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.

Karthik said: (Dec 13, 2015)  
@Siva.

What is the output got in above program?

Devendra said: (Sep 19, 2016)  
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.

Pranali said: (Apr 1, 2017)  
@Manoj.

There is no size for void datatype.

Still if we find sizeof(void) it will give 1 byte.(minimum size have to give).

Ashish Jha said: (Nov 15, 2017)  
I compiled in gcc. It is getting compiled. I am getting the following output:

6356744 6356741

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.