↗
Pointers For Notes
Tags
Things to learn before learning about pointers
What are pointers?
Manipulation with Pointers
Usage of Pointers
Character Array and Pointers
Multi-dimensional Array
Dynamic Memory
Pointers and Functions
Things to learn before learning about pointers
In a typical memory architecture, consider the entire main memory to be divided into
segments of 1 byte. Each byte of memory will have an address.
What happens when we declare a variable?
Let’s say you have the following declaration of variable.
int a;
The compiler will allocate some memory to the variable ‘a’, where the size of the memory
depends on the type of the variable. Here is the list of typical size of memory of different
types:
int — 4 bytes
char — 1 bytes
float — 4 bytes
A internal lookup table will keep track of the memory allocation situation.
What happens when we initialise/modify a variable?
Let’s say we assign a value to the variable when just declare.
Pointers For Notes 1
, a = 5;
The compiler will look into the lookup table. Find the corresponding address. Then write
the value to that memory in binary.
What are pointers?
What are pointers?
Pointers are variables, which they store the address of another variable as values.
In other words, the pointer points to some value of another variable
💡 Bear in mind that since pointers are variables, they will have addresses as well.
How to declare a pointer in C/C++?
int a; // normal variables
int *p; // we now have a pointer p
p = &a; // p now has the address of a
a = 5;
printf("the value of a: %d \n", a);
printf("the address of a: %p \n", &a); // &a should have the same value as p
printf("the value of p: %p \n", p);
printf("the address of a: %p \n", &p);
Explanation:
A pointer is declared by adding * in front of the variable name, behind the data type.
The asterisk sign indicates the variable name will be a pointer.
The data type can be any data type: int, float, char, or user-defined classes etc.
& followed by a variable name will return the address of the variable.
It is worth noting that &p will actually return the address to the pointer p itself, not
the content of p, which just happens to be an address.
Try running the above code to see the results.
Pointers For Notes 2
, 💡 Make sure you always initialise the pointer before using it, or else there will be
an runtime error
How to get the value correspond to the address in a pointer?
Up till this point, the pointer is merely another normal variable that stores another
variable’s address. What makes pointer special is that we can get the normal variables’
values through the pointer.
int a; // normal variables
int *p; // we now have a pointer p
p = &a; // p now has the address of a
a = 5;
printf("The value of a: %d \n", a); // a should be equal to *p
printf("Let's try to get the value of a through p: %d", *p);
Explanation:
The process of getting values of the address that pointers points to are called “de-
referencing”.
We do this by adding an asterisk * in front of the variable name:
This is not to be confused with the pointer declaration, despite having similar
syntax.
Try running the code to see the results
How to modify the value correspond to the address in a pointer?
We can also modify the value through a pointer.
int a; // normal variables
int *p; // we now have a pointer p
p = &a; // p now has the address of a
a = 5;
// Now I will change the value of a to 11
*p = 11;
printf("The value of a: %d \n", a); // a still equals to *p, but now instead of 5, it is 11
printf("Let's try to get the value of a through p: %d", *p);
Pointers For Notes 3
Pointers For Notes
Tags
Things to learn before learning about pointers
What are pointers?
Manipulation with Pointers
Usage of Pointers
Character Array and Pointers
Multi-dimensional Array
Dynamic Memory
Pointers and Functions
Things to learn before learning about pointers
In a typical memory architecture, consider the entire main memory to be divided into
segments of 1 byte. Each byte of memory will have an address.
What happens when we declare a variable?
Let’s say you have the following declaration of variable.
int a;
The compiler will allocate some memory to the variable ‘a’, where the size of the memory
depends on the type of the variable. Here is the list of typical size of memory of different
types:
int — 4 bytes
char — 1 bytes
float — 4 bytes
A internal lookup table will keep track of the memory allocation situation.
What happens when we initialise/modify a variable?
Let’s say we assign a value to the variable when just declare.
Pointers For Notes 1
, a = 5;
The compiler will look into the lookup table. Find the corresponding address. Then write
the value to that memory in binary.
What are pointers?
What are pointers?
Pointers are variables, which they store the address of another variable as values.
In other words, the pointer points to some value of another variable
💡 Bear in mind that since pointers are variables, they will have addresses as well.
How to declare a pointer in C/C++?
int a; // normal variables
int *p; // we now have a pointer p
p = &a; // p now has the address of a
a = 5;
printf("the value of a: %d \n", a);
printf("the address of a: %p \n", &a); // &a should have the same value as p
printf("the value of p: %p \n", p);
printf("the address of a: %p \n", &p);
Explanation:
A pointer is declared by adding * in front of the variable name, behind the data type.
The asterisk sign indicates the variable name will be a pointer.
The data type can be any data type: int, float, char, or user-defined classes etc.
& followed by a variable name will return the address of the variable.
It is worth noting that &p will actually return the address to the pointer p itself, not
the content of p, which just happens to be an address.
Try running the above code to see the results.
Pointers For Notes 2
, 💡 Make sure you always initialise the pointer before using it, or else there will be
an runtime error
How to get the value correspond to the address in a pointer?
Up till this point, the pointer is merely another normal variable that stores another
variable’s address. What makes pointer special is that we can get the normal variables’
values through the pointer.
int a; // normal variables
int *p; // we now have a pointer p
p = &a; // p now has the address of a
a = 5;
printf("The value of a: %d \n", a); // a should be equal to *p
printf("Let's try to get the value of a through p: %d", *p);
Explanation:
The process of getting values of the address that pointers points to are called “de-
referencing”.
We do this by adding an asterisk * in front of the variable name:
This is not to be confused with the pointer declaration, despite having similar
syntax.
Try running the code to see the results
How to modify the value correspond to the address in a pointer?
We can also modify the value through a pointer.
int a; // normal variables
int *p; // we now have a pointer p
p = &a; // p now has the address of a
a = 5;
// Now I will change the value of a to 11
*p = 11;
printf("The value of a: %d \n", a); // a still equals to *p, but now instead of 5, it is 11
printf("Let's try to get the value of a through p: %d", *p);
Pointers For Notes 3