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;
}
Yes
No
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.

Guilloip said:   4 years ago
That is not true.

A void pointer is treated as a byte pointer.

Ashish Jha said:   8 years ago
I compiled in gcc. It is getting compiled. I am getting the following output:

6356744 6356741

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).

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.

Karthik said:   10 years ago
@Siva.

What is the output got in above program?

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.

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.

Akhilesh said:   1 decade ago
j=k=&a;
error C2440: '=' : cannot convert from 'void *' to 'int *'.

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

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.

Vishu said:   1 decade ago
There are no errors in the above program. I compiled and I got the output.


Post your comments here:

Your comments will be displayed after verification.