Hey guys, I've got a couple of questions that are really stumping me on my review and I'm having a hard time finding the answer in my book. Could someone help me out? Here are the first two of the series:
Given the declarations
struct ListNode
{
float volume;
ListNode* next;
};
ListNode* ptr;
what is the data type of the expression ptr->next->next->volume?
a) ListNode*
b) ListNode
c) Float
d) *ListNode
e) none--the expression is invalid
a) ptr
b) *ptr
c) (*ptr).next
d) *((*ptr).next)
e) b and d above
If someone could tell me what the answers are to these I think I can figure the rest out, also a link to some good reference material would not go astray. Thanks!
What part of the questions are you having trouble with? Also, it seems you forgot part of the question on the second, as it's asking to select from a list of identifiers presumably but you didn't include that list.
In a chain of calls, the overall return type is the same as the last call, so for the first question, you should focus on FOO->volume. Therefore, the final type should be the same as the type of volume.
For the second question, remember that dereferenced pointers return a reference to the type they point to. So in the following:
1 2 3
int a(8);
int* b(&a);
a = *b; //Here *b is of type int
You can use that knowledge combined with what you know about chains of calls to help you figure the type of each option.
Random Note:
For BAR *FOO, *FOO technically returns BAR&, because you should be able to modify the original object that FOO points to and not a copy.
@Zhuge To answer your question, pretty much all of it. I'm having a hard time wrapping my head around pointers in general, it's like trying to figure out Jewish Dietary restrictions or something.
"You can have X cept when Y happens then you have to do Z unless it's a case of AA"