pointer subtraction

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

I am using version 3.3.2.

What can be assummed about the value of one pointer - a second pointer?

If you substract the lower address pointer from the higher would you expect to get a positive value?

If you subtract the same two pointers would you expect to see the same result each time?

Also what does the result actually represent?

Sample program attached which has caused me confusion.

Thanks,

Ivan
----------------------------------------------------------------------------------------------------------------------------------------------------------

#define COUNT 5

SymbolData* array[COUNT];

int
main(int, char **argv)
{
	for (int i = 0 ; i < COUNT ; ++i)
		array[i] = new SymbolData;
	
	SymbolData* testvar = array[0];

	for (int i = 0 ; i < COUNT ; ++i)
		{
		printf("testvar %ld array %ld a-b %ld b-a %ld\n", 
			testvar, array[i], testvar-array[i], array[i]-testvar);
		}

	return 0;
}

# output with gcc
testvar 236032 array 236032 a-b 0 b-a 0
testvar 236032 array 236272 a-b -1332920886 b-a 1332920886
testvar 236032 array 236512 a-b 1629125524 b-a -1629125524
testvar 236032 array 236752 a-b 296204638 b-a -296204638
testvar 236032 array 236992 a-b -1036716248 b-a 1036716248

# output with sun compiler
testvar 237424 array 237424 a-b 0 b-a 0
testvar 237424 array 237664 a-b -1 b-a 1
testvar 237424 array 237904 a-b -2 b-a 2
testvar 237424 array 238144 a-b -3 b-a 3
testvar 237424 array 238384 a-b -4 b-a 4


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux