[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

type casting

Posted on 2004-11-09
20
Medium Priority
?
561 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:xyoavx
  • 6
  • 4
  • 2
  • +3
15 Comments
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12540473
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
 
LVL 2

Expert Comment

by:arun80_inin
ID: 12541494
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12542892
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:xyoavx
ID: 12546092
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12546138
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
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12546529
> "... 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
 

Author Comment

by:xyoavx
ID: 12550498
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
 
LVL 4

Expert Comment

by:skypalae
ID: 12552831
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
 

Author Comment

by:xyoavx
ID: 12553574
Hi skypalae,
turbo c++ version 1.0  displays an error.
Regards,
xyoavx
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 100 total points
ID: 12553770
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
 
LVL 7

Assisted Solution

by:aib_42
aib_42 earned 100 total points
ID: 12578425
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
 

Author Comment

by:xyoavx
ID: 12827667
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12827700
>>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
 

Author Comment

by:xyoavx
ID: 12827869
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
 

Author Comment

by:xyoavx
ID: 12860781
Hello modulo,
What is the meaning of Forced accept ?
Regards,
xyoavx
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question