Last Updated on 2021-04-29 by Clay
A pointer is a variable that contains the address of a variable. Generally speaking, when we declare and assign a variable to the system today, the program will apply for space in the memory to store :
- Variable address (starting address)
- Variable value
- Variable name
Using indicators, we can declare a variable to point to the memory address of a specific variable (to get the address with the &
symbol).
What are the advantages of doing this? Pointers actually have many advantages, for example:
- Provide a method for passing in parameters for the function (for example, pass an array)
- Reduce copying of large blocks of memory and reduce waste of resources
- Support certain data structures (for example, Linked-List)
Of course, pointers are not without shortcomings, and sometimes the wrong way to use them can even cause the system to crash.
How to use Pointer
If you want to declare a pointer, you need to add the *
symbol after the data type.
data-type *p;
And how to operate pointer variable? Let’s look at a short sample code:
#include <stdio.h> // Main int main() { int x=1, y=2, z[10]; int *p; // p is a pointer to int p = &x; // p now points to x y = *p; // y is now 1 *p = 0; // x is now 0 p = &z[0]; // p is now points to z[0] return 0; }
Pointer and function parameters
As mentioned earlier when explaining the advantages of the pointer, the pointer provides a method to pass in parameters for the function. What does it mean?
We design a function swap(x, y) to exchange of the values of two variables x
and y
.
Wrong way
#include <stdio.h> void swap(int x, int y) { int temp = x; x = y; y = temp; } // Main int main() { int x=1, y=2; swap(x, y); printf("x: %d\ny: %d\n", x, y); return 0; }
Output:
x: 1
y: 2
As you can see, the values of x
and y
are not exchanged at all. This is because when the values of x
and y
are passed into swap(x, y)
, another copy is made, and the variables that are not exchanged in swap(x, y)
are the copied variables, which cannot really affect the variables passed in from outside. (Of course you can return the value of the exchanged variable, and then declare x
and y
again.)
Correct way
#include <stdio.h> void swap(int *px, int *py) { int temp = *px; *px = *py; *py = temp; } // Main int main() { int x=1, y=2; swap(&x, &y); printf("x: %d\ny: %d\n", x, y); return 0; }
Output:
x: 2
y: 1
It can be seen that using the pointer operation is like operating the original variable exchange value.
Pointer and Array
After declaring an array, our variable memory address is the memory address of the first element. Take a look at the sample code (%p
is the print memory address)
#include <stdio.h> // Main int main() { int a[3] = {1, 2, 3}; printf("a: %p\n", a); printf("&a[0]: %p\n", &a[0]); return 0; }
Ouptut:
a: 0x7ffee0c7c47c
&a[0]: 0x7ffee0c7c47c
Are the memory addresses exactly the same?
In addition, the index of the array is the “displacement” of the memory address of the first element. As long as the memory address of the first element is obtained and assigned to the pointer variable, each time it is increased by one unit, the memory location will be the same as the memory location of different elements in the array.
#include <stdio.h> // Main int main() { int a[3] = {1, 2, 3}; int *p = a; for (int i=0; i<sizeof(a)/sizeof(a[0]); ++i) { printf("p + %d: %p\n", i, p+i); printf("&a[%d]: %p\n", i, &a[i]); printf("===================================\n"); } return 0; }
Output:
p + 0: 0x7ffee102747c
&a[0]: 0x7ffee102747c
===================================
p + 1: 0x7ffee1027480
&a[1]: 0x7ffee1027480
===================================
p + 2: 0x7ffee1027484
&a[2]: 0x7ffee1027484
===================================
It is very easy to use pointer to get the values in the array:
#include <stdio.h> // Main int main() { int a[3] = {1, 2, 3}; int *p = a; for (int i=0; i<sizeof(a)/sizeof(a[0]); ++i) { printf("*(p + %d): %d\n", i, *(p+i)); } return 0; }
Output:
*(p + 0): 1
*(p + 1): 2
*(p + 2): 3
Character Pointer
In C programming language, a string is actually characters stored in arrays. And in the last section I have recorded how to manipulate the array through the pointer.
When using pointer variable to store a string, the content of the string cannot be changed (it is possible to use a character array to store it). Therefore, it is recommended to add const
when using pointer variable to store string.
#include <stdio.h> // Main int main() { const char *pText = "Hello"; char text[] = "world"; text[0] = 'W'; printf("%s %s\n", pText, text); return 0; }
Output:
Hello World
References
- https://www.tutorialspoint.com/cprogramming/c_pointers.htm
- https://www.programiz.com/c-programming/c-pointers
- https://openhome.cc/Gossip/CGossip/PointerAndArray.html
- https://openhome.cc/Gossip/CGossip/PointerAndString.html