English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn about pointers. What is a pointer, how to use it, and common errors that may occur when using it with examples.
Pointers are C and C ++ A powerful feature of programming. Before learning about pointers, let's learn about addresses in C programming.
If there is a variable var in the program, &var will provide its address in memory.
We have used addresses many times when using the scanf() function.
scanf("%d", &var);
Here, the value entered by the user is stored in the address of the var variable. Let's take a feasible example.
#include <stdio.h> int main() { int var = 5; printf("var: %d\n", var); // Note the use of & before var printf("address of var: %p", &var); return 0; }
Output result
var: 5 var address: 2686778
Note:When running the above code, you may get other addresses.
Pointers (pointer variables) are special variables used to store addresses rather than values.
This is how we declare pointers.
int* p;
Here, we have declared an int pointer p.
You can also declare a pointer in the following way.
int *p1; int * p2;
Let's take another example of declaring a pointer.
int* p1, p2;
Here, we declare a pointer p1And a regular variable p2.
Let's take an example.
int* pc, c; c = 5; pc = &c;
Here, we assign5And assign the address of c to the pointer pc.
To obtain the value pointed to by a pointer, we use the dereference operator (*For example:
int* pc, c; c = 5; pc = &c; printf("%d", *pc); // Output: 5
Here, the address of c has been allocated to the pointer pc. To get the value stored at that address, we used*pc.
Let's take an example.
int* pc, c; c = 5; pc = &c; c = 1; printf("%d", c); // Output: 1 printf("%d", *pc); // Output: 1
We have already allocated the address of c to the pointer pc.
Then, we change the value of c to1. Since the address of pc and c are the same, therefore*pc gets1.
Let's take another example.
int* pc, c; c = 5; pc = &c; *pc = 1; printf("%d", *pc); // Output: 1 printf("%d", c); // Output: 1
We have already allocated the address of c to the pointer pc.
Then, we use* pc = 1; Assign* pc is changed to1. Since the address of pc and c are the same, therefore c equals1.
Let's take another example.
int* pc, c, d; c = 5; d = -15; pc = &c; printf("%d", *pc); // Output: 5 pc = &d; printf("%d", *pc); // Output: -15
Initially, the address c assigned to the pointer pc: pc = &c;. Because c is5, so*pc gives us5.
Then, use the address d to assign to the pointer pc: pc = &d;. Since d is-15, so*pc gives us-15.
Let's take a feasible example.
#include <stdio.h> int main() { int* pc, c; c = 22; printf("c's address: %p\n", &c); printf("c's value: %d\n\n", c); // 22 pc = &c; printf("Pointer pc address: %p\n", pc); printf("Pointer pc content: %d\n\n", *pc); // 22 c = 11; printf("Pointer pc address: %p\n", pc); printf("Pointer pc content: %d\n\n", *pc); // 11 *pc = 2; printf("c's address: %p\n", &c); printf("c's value: %d\n\n", c); // 2 return 0; }
Output result
c's address: 003FFBF8 c's value: 22 Pointer pc address: 003FFBF8 Pointer pc content: 22 Pointer pc address: 003FFBF8 Pointer pc content: 11 c's address: 003FFBF8 c's value: 2
Usage of the program
int* pc, c;
Here, a pointer pc and a regular variable c are created, both of which are int type. Since pc and c are not initialized at the beginning, the pointer pc points to no address or a random address. The variable c has an address, but contains random garbage values.
c = 22;
This is assigning a value to the variable c22, that is to say,22is stored in the memory of the variable c.
pc = &c;
This will allocate the address of the variable c to the pointer pc.
c = 11;
This will11allocated to the variable c.
*pc = 2;
This will change the value at the memory location pointed to by the pointer pc2.
Assuming you want the pointer pc to point to the address of c. Then,
int c, *pc; //pc is an address, but c is not pc = c; // Error //&c is an address, but* pc is not *pc = &c; // Error //&c and pc are addresses pc = &c; //c and*pc value *pc = c;
This is an example that beginners often find confusing in pointer syntax.
#include <stdio.h> int main() { int c = 5; int *p = &c; printf("%d", *p); // 5 return 0; }
Why didn't an error occur when using it int *p = &c;?
This is because
int *p = &c;
is equivalent to
int *p: p = &c;
In both cases, we have created a pointer p (not*p) and assign it &c.
To avoid this confusion, we can use the following statement:
int* p = &c;
Now you know what a pointer is, you will learn about the relationship between pointers and arrays in the next tutorial.