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 Objective-C Basics (Retired) Pointers and Memory Structs

pointer in c

include <stdio.h>

int main ()

{ char *letter;

char c = 'k';

letter = &c;

printf(" value of c = %c and the value of letter = %c\n address of memory =  %s\n\n",c, *letter, letter);

}

what is the value of letter and why it is giving that answer??

1 Answer

Francisco Navarro
Francisco Navarro
14,185 Points

At the beginning it can be difficult to understand how pointers work but as you learn more in this course you'll notice they are not complicated at all.

You have to keep in mind that all variables are stored in cells as bytes and for accessing them you need to know how to get to that cell, so you need a direction (called pointer in development).

In C (and Objective-C as well) to indicate a variable contain a pointer instead of a value you have to put a "*" like your example: "char *letter;"

In your example you have a var char 'k' store in a cell. If you need to know who is pointing to that cell which the 'k' value is stored you must indicate it with a "&" so in: "char *letter = &c;" you have the value 'k' in var "c" and the pointer which is pointing to that var in "letter".

Finally in case you have a pointer (char * letter) and you need to know the value pointed by letter you must say: "char someVar = *letter" and you'll get the pointer instead of the value