casting trouble

M

macluvitch

Hello folks,

During developping I've met a problem this is how it looks like

I have an expression like this

(type *)var + 1

Wath heppens is I want to make a pointer to this

sth like ptr = &((type *)var + 1)
I know this is meaningless but I don't have the idea in mind

please could you give a suggestion ?

thanks
 
K

Karthik

macluvitch said:
Hello folks,

During developping I've met a problem this is how it looks like

I have an expression like this

(type *)var + 1
Assuming var is a pointer, I don't understand you are casting like
this (invites trouble, for sure).
Wath heppens is I want to make a pointer to this

sth like ptr = &((type *)var + 1)

You are incrementing the pointer variable by a scalar, (which sounds
fine). Why would you get the address of a pointer variable ?
That is UB. Did u want to do a dereferencing out here.
I know this is meaningless but I don't have the idea in mind

please could you give a suggestion ?

Please let us know the objetive clearly.
 
R

Richard Bos

macluvitch said:
I have an expression like this

(type *)var + 1

Wath heppens is I want to make a pointer to this

sth like ptr = &((type *)var + 1)
I know this is meaningless but I don't have the idea in mind

Well, it can't be done that way. You cannot take the address of an
expression result; expression results have no definite address in
memory. If you really need a pointer that points at that value, you'll
need to create an object of the appropriate type, assign the value to
that object, then get its address. Like this:

compatible_type var;
type *dummy, **ptr;

var=somevalue;

dummy=((type *)var + 1);
ptr=&dummy;

Since the value of a pointer does not depend on the value of what it
points at (think about it: does your address depend on your name? If you
change your name by deed poll, does your address change?), this is
correct, too:

compatible_type var;
type *dummy, **ptr=&dummy;

var=somevalue;

dummy=((type *)var + 1);

dummy needs to be of type "type *", because that is the type of the
cast, and therefore of the whole expression. ptr needs to be of type
"type **", because it points at dummy.

Richard
 
M

macluvitch

Thank you folks this sounds to be meaningfull :)

I've tried this before posting but this should be a bit slow
so I was thinking of a small hack , I mean some way to do it like any
simple pointer affectation like I've mentioned before
Anyhow, how this will probably work great why troubling oneself

Ok thank you folks this is very kind of you
Oh I've another question (sorry)
The casting is done by the compiler or the assembler
(no optimizing option is turning on)
90% it should be done by the compiler what do you think ?

bye
 
R

Richard Bos

macluvitch said:
I've tried this before posting but this should be a bit slow

How do you know? Have you measured it? Was the difference significant?
Does it occur often enough to have a measurable influence on your
program, or are you just worrying prematurely?
Ok thank you folks this is very kind of you
Oh I've another question (sorry)
The casting is done by the compiler or the assembler

That question does not make sense in the context of ISO C. There's no
reason to assume the existence of an assembler.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top