Sign in to follow this  
derek7

(anothertype*)pointer++

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this