C Pointers: What are they and how do you use them?
1 min readJun 3, 2022
What is a pointer?
- If you have a variable ‘var’ in your program, ‘&var’ will give you the address in memory of the variable ‘var’.
- Pointer variables are special variables that are used to store addresses rather than values.
- A pointer is a variable that stores the memory address of another variable as its value.
- A pointer variable points to a data type (like
int
) of the same type, and is created with the*
operator. The address of the variable you're working with is assigned to the pointer:
How do you declare a pointer?
int* p;
The above statement declares a pointer ‘p’ of ‘int’ type.
int *p1;
int * p2;
The above are also different ways to declare a pointer.
How do you assign addresses to pointers?
int* pc, c;
c = 5;
pc = &c;
5 is assigned to the ‘c’ variable. The address of ‘c’ is assigned to the pointer variable ‘pc’.
How do you get the value of a pointer?
We use the * operator.
int* pc, c;
c = 5;
pc = &c;
printf("%d", *pc); // Output: 5
To get the value stores in the ‘pc’ pointer, we use *pc. * is also called the dereference operator.