C++ homework help (pointers)
PostPosted: 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...
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;
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
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