(anothertype*)pointer++

Started by
9 comments, last by kloffy 18 years ago
first pointer++; then do cast . but how to do cast first? ((anothertype*)pointer)++ do not work
Advertisement
Hey bud,

Something like this:

(type*)++pointer;


Hope that helps,

Dave
Move up to C++.

Seriously, if you can't figure out how to use some dustry corner of a langage to do what you think you want, it's just a big red flashing light with an annoying claxon going off telling you you've got the wrong design. You will probably be happier going with that and redesigning rather than forcing the compiler against its better jusdgement. In the long run you will be thankful.

Oh, and you could do it in two steps. The first line is your reinterpret_cast<>(), the second line increments your pointer. Or are you trying for first place in some obfuscated C code contest?

Stephen M. Webb
Professional Free Software Developer

Quote:Original post by Bregma
Move up to C++.

Seriously, if you can't figure out how to use some dustry corner of a langage to do what you think you want, it's just a big red flashing light with an annoying claxon going off telling you you've got the wrong design. You will probably be happier going with that and redesigning rather than forcing the compiler against its better jusdgement. In the long run you will be thankful.

Oh, and you could do it in two steps. The first line is your reinterpret_cast<>(), the second line increments your pointer. Or are you trying for first place in some obfuscated C code contest?


What are you talking about?

It is a simple case of moving the ++ in front of the pointer. Maybe he just doesn't know the difference between post and pre-increment.

Can you explain what design you are talking about?

Dave
Quote:Original post by Dave
Hey bud,

Something like this:

(type*)++pointer;


Hope that helps,

Dave


this way, still ++ operation first done, second cast operator done.

I wanna first cast ,and then do ++ operator
So make it two statements:

int *foo = whatever;char *bar = reinterpret_cast<char *>(foo);++bar;



Remember that in C and C++, using ++ on a pointer is not the same as adding 1 to the memory address - it's adding 1 * sizeof(type pointed to) to the address. So if (foo = 0x1000), foo + 1 is not 0x1001 - it's 0x1004 (on a 32-bit platform anyways) because sizeof(int) is 4.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Quote:Original post by ApochPiQ
So make it two statements:

int *foo = whatever;char *bar = reinterpret_cast<char *>(foo);++bar;



it seem it can not be done in a statement.

Quote:Original post by ApochPiQ

Remember that in C and C++, using ++ on a pointer is not the same as adding 1 to the memory address - it's adding 1 * sizeof(type pointed to) to the address. So if (foo = 0x1000), foo + 1 is not 0x1001 - it's 0x1004 (on a 32-bit platform anyways) because sizeof(int) is 4.
that is exactly what I need - cast
Quote:Original post by Dave
Can you explain what design you are talking about?


The design requiring a pointer to an object of one type to be cast to a pointer to an object of an unrelated type. If the pointers are to related types, the cast is not needed.

In C++, this is (almost) never needed. It is (almost) always a sign of a bad design. The (almost) is because on rare occasions, almost all limited to the unmarshalling of serialized data, you may need to do so, but then you're not going to be incrementing the pointers.

It may be necessary to cast from a void pointer when you're dealing with API firewalls, but you don't need to increment such a pointer. It may be necessary to downcast a pointer on rare occasions (ie. use dynamic_cast<>()), especially when dealing with a third-party library, but then you don't need to increment the cast pointer.

That's the design flaw I'm talking about.

Stephen M. Webb
Professional Free Software Developer

Quote:Original post by Bregma
Quote:Original post by Dave
Can you explain what design you are talking about?


The design requiring a pointer to an object of one type to be cast to a pointer to an object of an unrelated type. If the pointers are to related types, the cast is not needed.

In C++, this is (almost) never needed. It is (almost) always a sign of a bad design. The (almost) is because on rare occasions, almost all limited to the unmarshalling of serialized data, you may need to do so, but then you're not going to be incrementing the pointers.

It may be necessary to cast from a void pointer when you're dealing with API firewalls, but you don't need to increment such a pointer. It may be necessary to downcast a pointer on rare occasions (ie. use dynamic_cast<>()), especially when dealing with a third-party library, but then you don't need to increment the cast pointer.

That's the design flaw I'm talking about.


what do I need to do the job that implementing such a pointer.

Ok cool Bregma :).

How about:

++( (cast)ptr );

Dave

This topic is closed to new replies.

Advertisement