Pointer
a box saying where another box is
Pointer
Addresses
When you declare a variable, the compiler stores it at a memory location.
Given a variable named var, we can determine its memory address with &var.
#include <stdio.h>
int main() {
int var = 5;
printf("value of var : %d\n", var);
printf("address of var : %p\n", &var);
return 0;
}If you run the code above, you'll get something like this:
value of var : 5
address of var : 0x7ffe36dfeab8What is a Pointer?
pointer : A variable for storing addresses of a specific type.
You use * to declare pointer variables.
You can access the variable pointed by the pointer (called dereferencing). This is done by putting * in front of the pointer.
Output:
Sample Code
Output:
Pointers As Arguments of Functions
You can pass pointers as arguments of functions just like other variables.
because you pass addresses as parameters, you can change the pointed variables within functions.
Last updated