This is a Microsoft Placement Question : void main() { void *ptr; char *a='A'; char *b="TAN"; int i=50; ptr=a; ptr=(*char)malloc(sizeof(a)); printf("%c",*ptr); ptr=i; ptr=(*int)malloc(sizeof(i)); printf("%d",++(*ptr)); ptr=b; ptr=(*char)malloc(sizeof(b)); printf("%c",++(*ptr)); } The answer for this is A51AN... Can anyone explain how this answer is obtained?
This is seriously a microsoft "placement question"? void main(), dereferencing a void pointer without a cast, retriceignment making a pointer from an integer without a cast.
assignment*
Also, it has syntax errors.
Forget the syntax errors, can anyone explain the answer? it's surly a microsft placement question : http://ow.ly/6RtYF
It seems to be C code. If it has syntax errors, then the answer is that it doesn't print A51AN; it won't even compile!
Aside from the syntax errors and other misuses, the idea behind the code is the concept of pointers. It first prints the 'A' that was stored in a char* (although the way it is done in the code is incorrect) Then, it prints 51 (on the same line), by dereferencing and prefix-incrementing the value 50 that was stored on an int (although the pointer itself stored the value of i instead of its address, another big nono). Finally, it prints the characters after the first character in the "TAN" string literal (so it prints "AN") through pointer arithmetic (although he used the indirection operator for some reason). If it worked as intended, the output would indeed be "A51AN" I have no idea what all the mallocs were meant for.
Here's a better way to do the same thing, using the same ideas. http://ideone.com/lKhdc it does print A51AN, indeed!
Join our real-time social learning platform and learn together with your friends!