• C

type casting

Hello,
Please explain, in  details, how type casting is done. For example, in the following case:
int *p;
float *f1;
f1=(float *)p;
How this type casting is evaluated ? does it modify the pointer's symbol table ?
Only profound explanations will be accepted.
Regards,
xyoavx
xyoavxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

brettmjohnsonCommented:
int *p;    // p is a pointer to an int.  It, once set, will hold the memory address of an integer.
float *f1;      // f1 is a pointer to a float.  It, once set, will hold the memory address of a floating point number.

A cast coerces one type to another, overriding the compiler's attempt to ensure you perform operations
on suitably like types.

f1=(float *)p;            // this cast makes the pointer f1 point to the same memory location that the pointer p does.

Now there are two problems with the above cast.  First, p was never assigned a value, so does not actually
point to anything.  [Actually, it could point to a random memory location.]  Second, sizeof(int) is not
guaranteed to be equal to sizof(float) [indeed, the are usually not the same size].


0
arun80_ininCommented:
If u want to conver the value of pointer p to float

then u have to give the statement like this
*f1=float(*p)


Note : for pointer variable
the variable name represents address
and *(varname) represents the value
0
PaulCaswellCommented:
A type cast (in C) changes NOTHING in the code. It is an instruction to the compiler to not bother with the usual type checking. That is why casting is so dangerous and should only be used carefully.

Paul
0
The Lifecycle Approach to Managing Security Policy

Managing application connectivity and security policies can be achieved more effectively when following a framework that automates repeatable processes and ensures that the right activities are performed in the right order.

xyoavxAuthor Commented:
Hello Programmers
Thanks for your comments.They don't answer my question. I understand that type cast is implementred by writing something, somewhere. I would like to know, what is written, where it is written and how can I see it.
Remember ? my original question was:
"How this type casting is evaluated ? does it modify the pointer's symbol table ?"
Regards,
xyoavx

0
PaulCaswellCommented:
Xyoavx,

As I said, in 'C' nothing changes except the syntax checking and perhaps some sizes. In C++, you can write some code that will be executed when a cast happens but not in C.

Paul
0
brettmjohnsonCommented:
> "... Does it modify the pointer's symbol table ?"

I do remember that was your original question.  
We didn't answer it because the question is meaningless.
The pointer does not have a symbol table to modify.

The pointer may have an entry in the object file's symbol
table, but that entry describes the address of the pointer
variable itself, not its value.  The cast provided assigns a
value to the pointer - it does not change that pointer's
location in memory.



0
xyoavxAuthor Commented:
Hello
Thanks for the comments. If, the cast does nothing, why am I enforced, by the compiler, to use it ? Why f1=p; will display an error ?
Regards,
xyoavx
0
skypalaeCommented:
Hi xyoavx,

int *pi ;
float *pf ;
pf = pi ;

will not produce any error (VC.NET 2003). However it produces two warnings:
warning C4133: '=' : incompatible types - from 'int *' to 'float *'
warning C4700: local variable 'pi' used without having been initialized

but these were allready mentioned

Cheers! S.
0
xyoavxAuthor Commented:
Hi skypalae,
turbo c++ version 1.0  displays an error.
Regards,
xyoavx
0
PaulCaswellCommented:
The error you are being given is a mechanism to remind you that you are almost certainly doing something wrong such as trying to make a float pointer pointo to an integer. Casting is your way to tell the compiler that this IS what you mean to do and that you will handle the other issues arising from this such as the fact that an integer is smaller than a float.

Paul
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aib_42Commented:
Indeed, the cast modifies something, but that something has nothing to do with the program itself. As it has been said, the cast merely instructs the computer to change the expression's type.

int some_integer = 3;
int *some_pointer = &some_integer;

Consider the following (int i; float f;):

i = *some_pointer;
or
i = *((int *)some_pointer);

These will read the value at the location where some_pointer is pointing to. Since the type is "pointer to int", the bits and bytes at that location will be treated as an integer, the right-hand side evaluating to an integer, and the whole expression assigning it to i. (note that the cast is redundant since the type of the expression "some_pointer" is already (int *).

Now consider:
f = *((float *)some_pointer);

Again, the memory location that some_pointer is pointing to will be read. However, this time, the expression ((float *)some_pointer) will be of type "pointer to float", and the * operator will treat the bytes as those belonging to a float. The internal bit-by-bit representation of the integer 3, whatever that may be, will be treated as being the internal representation of a floating-point variable, taking an unknown or implementation-specific float value, whatever that may be. (Note that more bits/bytes than the actual size of an int may be read, if floats take up more space in memory in your implementation. Same goes if floats take up less space; then less bits/bytes will be read.)

Granted, the type of the cast will change how your program behaves, but that is only because the * operator takes the type of the pointer expression into account while dereferencing the pointer. The action ("dereference some_pointer and assign it to a variable") will be the same in both cases.

Note that "some_pointer" will always be a pointer to int - no compiler tables referring to some_pointer will be ever modified. However, the expression ((float *) some_pointer) is more or less temporary, and since there is no need to store its type in a table (the compiler can always re-evaluate it), it will not be stored in a table, and no tables will be modified. Summarized: A variable's type is stored in an internal table, is defined when the variable is declared, and is constant. Expressions will have their immediate types evaluated according to the just mentioned table and casts.

Again:
int i = 3;

The expression "(i)" will be of type "int" and of value "3".
The expression "(i+1)" will be of type "int" and of value "4".
The expression "((float) i)" will be of type "float" and of value "3.0".

Note that neither the type nor the value of 'i' was ever modified above, so "(i)" will still be of type "int" and of value "3" even after using the expressions above.

Now there are ways to change the value of 'i', such as (i)=4; or ++(i);, but they are not of our concern.
There are no ways to change the type of 'i'.
0
xyoavxAuthor Commented:
Hello Brettmjohnson
You wrote:
The pointer may have an entry in the object file's symbol
table, but that entry describes the address of the pointer
variable itself, not its value.  
My question: If that entry describes only the address of the pointer variable
so, where is written the number of bytes that should be skipped by a pointer, p, when writing p++?
Regards,
xyoavx
0
PaulCaswellCommented:
>>If that entry describes only the address of the pointer variable so, where is written the number of bytes that should be skipped by a pointer, p, when writing p++?
The compiler knows at compile time the size of the item the pointer points to so p++ will result in adding that size to the pointer.

Paul
0
xyoavxAuthor Commented:
Hello PaulCaswell
What is the meaning of: "The compiler knows at compile time the size of the item the pointer points to" ?
I guess that this size is written somewhere but I don't know where. Am I wrong ?
Regards,
xyoavx
0
xyoavxAuthor Commented:
Hello modulo,
What is the meaning of Forced accept ?
Regards,
xyoavx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.