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 2 of 2.

Manish said:   1 decade ago
The void pointer can't perform arithmetic operation.

Sam said:   1 decade ago
It will be compiled. As it should display the memory locations next to the location of a.

Megala said:   1 decade ago
I think the statement
j=k=&a;
is wrong

It will generate compiler error.

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.

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???

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.

Ankit Anand said:   2 decades ago
printf statement should be:-

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

else everything fine on LINUX


Post your comments here:

Your comments will be displayed after verification.