Would the following program compile? main() { int a=10,*j; void *k; j=k=&a; j++; k++; printf("n%u%u",j,k); }

A) Yes
B)

No


Showing Answers 1 - 13 of 13 Answers

ranjithkumar

  • Feb 17th, 2006
 

no.because we can't apply arithmatic operators on void pointers.but we can on int or fundamentaldatatypes except void .

neetu

  • Feb 28th, 2006
 

It will compile..but prints some garbage value.

  Was this answer useful?  Yes

louisyang

  • Mar 29th, 2006
 

It depends on compilers.

If you use ISO C compiler such as gcc, it will compile and work.

But if you use ISO C++ compiler such as g++, it wont compile since any operation on void pointer is not allowed. Explicit casting on void is required.

Amit singh

  • Jul 22nd, 2006
 

Yes the prog will compile.Compilation error occurs here only if we try to access (dereference) the modified pointer variable after arithmetic operations.As here we are not accessing the values they point at after modification i.e we are not dereferencing it will compile and print the address they contain

  Was this answer useful?  Yes

aniljoo

  • Feb 12th, 2007
 

no it will not work

  Was this answer useful?  Yes

abhimanipal

  • Jul 29th, 2009
 

test.c:10: error: invalid conversion from `void*' to `int*'
test.c:12: error: ISO C++ forbids incrementing a pointer of type `void*'

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions