Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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