May 20, 2004 1:45 PM
Pointers are very useful but not easy to understand in C/C++. Try to predict the output of the following code:

int* a;
int* b;
a=(int *)1000;
b=(int *)2000;  
printf("Difference: %d",b-a);

You are wrong if you expect the output to be 1000. Generally, the output of the program will be:

(2000 - 1000) / sizeof(int)

The output of the above program varies depending upon the C implementation on the machine. If you compile and run this using Turbo C on a intel dos/windows machine, the output will be 500. But if you are compiling this using gcc and running on a intel based *nix system, the output would be 250. The reason for this is that the size of integer variable is 2 bytes on a Turbo C implementation while in gcc, its 4 bytes.


Copyright © 2004-2011 Anirudh Sasikumar. All rights reserved.
Last Updated: January 21, 2005 4:37 PM