rosieblue
728x90

Before getting started ..
// This course is going to deal with pointer basics in C for beginners. 
// EVERY C COURSE WILL BE IN ENGLISH.
// Every question in English or Korean is always welcome :)
 
Hi you all! This is Hannah, your new programming mate!
And today we're going to learn about the meaning of pointer and addresses.
 

POINTER and ADDRESSES

pointer : A pointer is a 'variable' which stores 'address' -> Value of pointer is 'address'.
Then, whose address will be stored in pointer? Every data type can be pointed by a pointer, even a pointer can points to another pointer too, which is called 'multiple indirection'.
 
Let me show you a declaration of pointers.

<data type> * <pointer variable's name>;
int * num;

-> Here 'num' is a pointer variable that may 'point to' (that is, it may store the address of) an integer.
WHEN POINTER 'P' POINTS TO 'A', AND IF AND ONLY IF 'P' STORES ADDRESS OF 'A'.
 
and what is the 'address' exactly?
 
Address is a memory location where the data is stored=A location in memory.
We can obtain the address of some data by using ampersand '&'.

int * num; //'num' points to integer variable.
int i=1; //declaration and initialization of integer variable 'i'.
num=&i; //By using '&', the address of 'i' will be stored in 'num'.

This code assigns the address of 'i' variable to 'num' variable.
It is important that '&i' returns address, not 'i' itself(=1)! As 'num' stores the address(value of num is address), we don't need data casting in 'num=&i'.
 
Let me show another code here.

printf("i=%d, num=%d, address of i=%d, address of num=%d", i, num, &i, &num);

Computer will print out :

i=1, num=111111, address of i=111111, address of num=222222.

In this result, we can now convince that address of i is num!
 

INDIRECTION (DEREFERENCE)

What if we want to know the value of data itself? In this case, use operator '*' which is called indirection or dereference operator. Be careful that '*' here is not same in declaration of pointer like int*i;.
 
Indirection of dereferencing is accessing the value of data stored at the address which is pointed by a pointer.
 
 

int num;
int *numPtr;
num=100;
numPtr=&num;
printf("%d", *numPtr);

Result will be like: 100.
 
Quiz :
What does pointer variable store?

profile

rosieblue

@Rosieblue

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!