Page 1 of 1

C++ homework help (pointers)

PostPosted: Tue Sep 12, 2006 11:33 am
by Slater
Hmm... Pointers aren't too hard as long as there isn't any type casting involved, I found out. Unfortunatly, my homework involves some knowledge of that. It's to take this file, fix the errors (make it so it compiles; this link intentionally doesn't compile), and comment the source so it says what each section of code does.

For the most part, it's an easy assignment. Getting the thing to compile was very easy. However, a few lines I'm not sure how to comment since I'm not entirely sure what it does. For example...

Code: Select all
ptrK = (int *) ptrB;


so there's a float-type pointer (ptrB) and an int-type pointer (ptrK). Now, am I right to say that the above line of code converts (type-casts) ptrB to int type and assigns that value to ptrK?
If so, what does that mean? ptrK is pointing to a new memory address?

When I run the program, I do find that they do point to the same address, but that something funky happened to *ptrK o.O;
Code: Select all
ptrB = 0xffbefc00
*ptrB = 1
ptrK = 0xffbefc00
*ptrK = 1065353216


can someone explain to me why ptrK equals that? are ints and floats really so much different on the binary level? (they're both 4-byte types, right?)

Edit: For those of you who don't wanna fish through the whole code, int b was initilized as 1, and ptrB wasn't changed at that point in that point of the code. Thus, *ptrB = b = 1

PostPosted: Tue Sep 12, 2006 1:54 pm
by Technomancer
Slater wrote:Hmm... Pointers aren't too hard as long as there isn't any type casting involved, I found out. Unfortunatly, my homework involves some knowledge of that. It's to take this file, fix the errors (make it so it compiles]ptrK = (int *) ptrB;[/code]

so there's a float-type pointer (ptrB) and an int-type pointer (ptrK). Now, am I right to say that the above line of code converts (type-casts) ptrB to int type and assigns that value to ptrK?
If so, what does that mean? ptrK is pointing to a new memory address?

When I run the program, I do find that they do point to the same address, but that something funky happened to *ptrK o.O;
Code: Select all
ptrB = 0xffbefc00
*ptrB = 1
ptrK = 0xffbefc00
*ptrK = 1065353216


can someone explain to me why ptrK equals that? are ints and floats really so much different on the binary level? (they're both 4-byte types, right?)

Edit: For those of you who don't wanna fish through the whole code, int b was initilized as 1, and ptrB wasn't changed at that point in that point of the code. Thus, *ptrB = b = 1


int and float data types are structured quite differently from each other, which would account for your getting such very different outputs. Basically, a floating point number is stored as a mantissa and a signed exponent, while the integers basically represent the number directly. If you really want to find exactly out what happens, you'll have to look up the particular formats.

PostPosted: Wed Sep 13, 2006 11:34 am
by Kaligraphic
Indeed. That's actually the whole reason that typecasting is necessary - binary incompatibility. If you want to see what the numbers really look like, try writing a program to store the same number as an int and as a float, then have it output the individual bits. You may find the differences to be interesting.

PostPosted: Thu Sep 14, 2006 11:57 pm
by Slater
Right, just as I suspected. Thanks guys, the end product of this assignment was fairly good. Professer Bierman used it as the prime example of how it should have been done in class today so hopefully I scored well :)