adding pointers portably (in C)

Programming, for all ages and all languages.
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

I'm pretty sure even Microsofts x86_64 compiler doesn't do this. It's very common to have long long always 64-bit and the long type as whatever the native type of the processor is. They would both be 64-bit then, which this line forbids.
iirc, in the MS x86-64 compiler, long is 32bits (not 64)
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

JAAman wrote:
I'm pretty sure even Microsofts x86_64 compiler doesn't do this. It's very common to have long long always 64-bit and the long type as whatever the native type of the processor is. They would both be 64-bit then, which this line forbids.
iirc, in the MS x86-64 compiler, long is 32bits (not 64)
Hm... ok, it would still imply (and make you think) that GCC isn't compliant with C/C++, where it really is.

Just a small note on the original topic, you can add them portably by casting them to intptr_t and adding them. There's just a small catch, why add two pointers? In all cases you have an object at a given location and an index into the object, you never add the location of one object to the location of another.

You can have slightly more complex ones, where A=B+C-D or such, but in that case C-D would be a ptrdiff_t and you can logically add a ptrdiff_t to an intptr_t to get another intptr_t.

Why do you want to add two pointers?
Post Reply