Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Confused About Pointer Question

The final question in the C Pointers section asks about the value of item, given this code:

Product tshirt; Product *item = &tshirt;

I chose the answer that said "Item contains the memory address of tshirt." The correct answer is actually: "Item points to the same memory address as tshirt and so contains the same value of tshirt."

Can someone explain that to me? Isn't tshirt a null value? Wouldn't you need to dereference item in order to get the value of tshirt?

3 Answers

I disagree with the answer to this quiz. A pointer is just a 32-bit variable. When you say Product *item = &tshirt, you are essentially creating a 32-bit variable named item and giving it the 32-bit memory address of tshirt. "Pointing to a memory address" just means that your pointer variable contains the memory address of the thing that you're pointing to.

In my opinion, the answer "Item contains the memory address of tshirt" is completely correct. In fact, if you run the program using a debugger or printing the address of your variables, you can see that that answer is correct.

The answer "Item points to the same memory address as tshirt and so contains the same value of tshirt" is partially correct. item does point to the same memory address as tshirt, since tshirt is essentially just the address of the first byte of the structure. However, item and tshirt do not contain the same value. If they did, you wouldn't need to use the dot operator to access tshirt.field1, but have to use the arrow operator to access item->field1. I agree that this is a poorly worded question, however Justin's answer is 100% correct.

EDIT: I'll clarify what I said above a little bit. When you declare a structure like tshirt, you get a block of memory that stores all of your structure's fields, one after another. So for example, suppose you declare a structure like this.

typedef struct Product
{
   int productId;   // this is 4 bytes
   char description[30];   // this is 30 bytes
   double price;   // this is 8 bytes
} Product;

The Product structure is 42 bytes. If you say Product tshirt = new Product();, then you get a 42-byte block of memory. The memory location of the variable tshirt is the first byte of those 42-bytes. Your compiler knows that if you say tshirt.productId then you want the first 4 bytes of your structure, starting at the first byte of tshirt. Similarly, if you say tshirt.price, then the compiler knows that you want 8 bytes, starting at the thirty-third byte of tshirt (since price is 33 bytes away from the start of the structure).

Now for the situation where you have the item pointer pointing to tshirt. You can't say item.productId because you don't want the first 4 bytes of item. You want the first four bytes of the thing that item points to. So first you go to the address that is stored in item and then you take the first 4 bytes of that. Contrast that with the above scenario where you just want the first 4 bytes of tshirt. This is a little more advanced than what's in that lesson, but that's what the different is between the dot operator and the arrow operator.

Here's some code from Wikipedia that further demonstrates that Justin's answer is correct.

struct point {
   int x;
   int y;
};
struct point my_point = { 3, 7 };
struct point *p = &my_point;  /* To declare and define p as a pointer of type struct point,
                                 and initialize it with the address of my_point. */

(*p).x = 8;                   /* To access the first member of the struct */
p->x = 8;                     /* Another way to access the first member of the struct */

yeah looks like its just a badly worded question.

edit: badly worded queston

I thought pointers were variables that store memory addresses and that, to obtain the value they are pointing to, you have to explicitly dereference it by using * (in this case, *item). Where am I misunderstanding? Thanks very much for your reply. I really appreciate it.

you are correct about dereferencing using *. In this quiz question it appears that when they ask about the value of item, they really mean what is the value of the object that item points to. I can see how you got confused as the question isnt worded very well. Could you link the actual quiz?

Thanks a lot. Here's the quiz:

http://teamtreehouse.com/library/structs

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

You are not misunderstanding. I think that quiz has been worded incorrectly. I have gone ahead and fixed it. Thank you for bringing it to our attention.