Understanding (int*), casts

I can't understand pointer casts ( C Style ). Implicit casts are next, but C pointer casts don't make sense to me.

What does this mean (int*)&var or (char*)&var the fact that the dereference operator comes after the type but inside the parenthesis mean that a memory address will be returned, a pointer will be returned or something else

I'm just not understanding what is returned and two read the two expressions above.

the fact that the dereference operator comes after the type but inside the parenthesis mean that a memory address will be returned,


It means it's a pointer. IE: it's part of the type:

1
2
int foo;  // <- foo is of type 'int'
int* ptr;  // <- ptr is of type 'int*'.  That is, a pointer to an 'int' 


When doing a C-style cast, you are basically taking something that is one type, and telling the computer to treat it as if it were of a different type:

1
2
3
4
5
int* foo;  // <- foo is a pointer to an int
char* bar;  // <- bar is a pointer to a char

bar = (char*)foo;  // <- the cast here tells the computer, "Yeah, I know foo isn't a pointer
    // to a char, but I want you to treat it like one anyway" 
Ok, I think this is where I am getting confused.

The line where you have: bar = (char*)foo; (char*)foo is returning an address if I am correct. So bar with no dereferencing symbol is receiving an address as it is expecting one.

1
2
3
int *pointer;
*pointer = Returns the thing pointed at;
pointer = the address of the thing pointed at;


So if bar is expecting an address, why are we saying that it is going to receive a pointer by placing (char*) in front of foo?
"Address" and "pointer" are synonyms.
foo is a pointer, and (char *)foo is a pointer, and bar is a pointer. bar = (char *)foo assigns a pointer to a pointer. The cast is just a way to tell the compiler "don't complain about incompatible types; I know what I'm doing".
Hmm. Well, then I can't understand why the below doesn't work.
I cast a float to an int. If I then use the line *mnt = (int*)&flt; I get a compile time error. If I don't use the * for mnt
mnt = (int*)&flt;then it compiles but the output for *mnt outputs a garbage value and mnt outputs an address as it should.

It comes down to a fundamental misunderstanding of casting and I'm trying to figure out what it is.

I'm not just trying to solve the below (because I can make this work in other ways), but using pointers adds a layer of confusion in relation to how to read the sequence of events in the casting process.




1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{

float flt=  6.5;
int *mnt = new int;

  mnt = (int*)&flt;

  cout << mnt << endl;
  cout << *mnt << endl;


}
Last edited on
If I then use the line *mnt = (int*)&flt; I get a compile time error.
Yes. *mnt is an int. (int*)&flt is a pointer.

the output for *mnt outputs a garbage value
That's the result of interpreting the bytes stored at &flt as if they were the bytes for an integer. Try assigning a value to *mnt and see how flt changes.
In general, floating point representations are completely unrelated to integer representations.

By the way, this:
1
2
int *mnt = new int;
mnt = (int*)&flt;
leaks memory.
These don't:
1
2
3
int *mnt = new int;
delete mnt;
mnt = (int*)&flt;

1
2
int *mnt;
mnt = (int*)&flt;

 
int *mnt = (int*)&flt;
yes, but none of these examples return the value of flt in mnt.

How would I cast flt to mnt in this case? What would be the correct way to do it?
If you simply want mnt to contain an integer approximation of flt, the correct way is simply mnt = (int)flt
Thanks for the help helios :)
Topic archived. No new replies allowed.