C++ homework help (pointers)

Homework giving you a headache? Math gives you a migraine? Can't quite figure out how to do something in photoshop? Never fear, the other members of CAA share their expertise in this forum.

C++ homework help (pointers)

Postby Slater » Tue Sep 12, 2006 11:33 am

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
Image
User avatar
Slater
 
Posts: 2671
Joined: Sat May 22, 2004 10:00 am
Location: Pacifica, Caliphornia

Postby Technomancer » Tue Sep 12, 2006 1:54 pm

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.
The scientific method," Thomas Henry Huxley once wrote, "is nothing but the normal working of the human mind." That is to say, when the mind is working; that is to say further, when it is engaged in corrrecting its mistakes. Taking this point of view, we may conclude that science is not physics, biology, or chemistry—is not even a "subject"—but a moral imperative drawn from a larger narrative whose purpose is to give perspective, balance, and humility to learning.

Neil Postman
(The End of Education)

Anti-intellectualism has been a constant thread winding its way through our political and cultural life, nurtured by the false notion that democracy means that my ignorance is just as good as your knowledge

Isaac Aasimov
User avatar
Technomancer
 
Posts: 2379
Joined: Fri Jun 13, 2003 11:47 am
Location: Tralfamadore

Postby Kaligraphic » Wed Sep 13, 2006 11:34 am

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.
The cake used to be a lie like you, but then it took a portal to the deception core.
User avatar
Kaligraphic
 
Posts: 2002
Joined: Wed Jul 21, 2004 12:00 pm
Location: The catbox of DOOM!

Postby Slater » Thu Sep 14, 2006 11:57 pm

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 :)
Image
User avatar
Slater
 
Posts: 2671
Joined: Sat May 22, 2004 10:00 am
Location: Pacifica, Caliphornia


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 42 guests