Pointers in C language are variables that store the address of another variable. This variable can be an int, char, array, function, or any other pointer type. The size of a pointer depends on the computer architecture. However, in a 32-bit computer architecture, the size of a pointer is 2 bytes.
Consider the following example to define a pointer that stores the address of an integer.
int n = 10; int* p = &n; // Pointer variable p points to the address of integer variable n
To declare a pointer in C language, the * (asterisk) symbol is used. It is also called an indirect pointer, used for dereferencing the pointer.
int* a; // Pointer to int
char* c; // Pointer to char
Pointer Example
Below is an example using pointers to print the address and value.
The pointer variable stores the address of the variable number, which is fff4. The value of the number variable is 50. However, the address of the pointer variable p is aaa3.
By using the * (indirection operator), we can print the value of the pointer variable p.
👇 Click to receive 👇
👉 C Language Knowledge Resource Collection
Let’s look at the pointer example explained above.
#include <stdio.h>
int main() {
int number = 50;
int* p;
p = &number; // Store the address of the number variable
printf("The address of variable p is: %x\n", p); // p contains the address of number, so printing p will get the address of number
printf("The value of variable p is: %d\n", *p); // As we know, * is used to dereference the pointer, so if we print *p, we will get the value stored at the address contained in p
return 0;
}
Output:
The address of variable p is: fff4
The value of variable p is: 50
Pointer to Array
int arr[10];
int* p[10] = &arr; // Pointer variable p points to the address of the integer array arr
Pointer to Function
void show(int);
void (*p)(int) = &display; // Pointer p points to the address of the function
Pointer to Struct
struct st {
int i;
float f;
} ref;
struct st* p = &ref;
Advantages of Pointers
Pointers reduce the amount of code and improve performance. They are used for retrieving strings, trees, etc., and are used with arrays, structures, and functions.
We can use pointers to return multiple values from a function.
They allow you to access any memory location in the computer's memory.
Uses of Pointers
In C language, pointers have many applications.
1) Dynamic Memory Allocation
In C language, we can use malloc() and calloc() functions to dynamically allocate memory, which involves the use of pointers.
2) Arrays, Functions, and Structures
Pointers are widely used in C language for arrays, functions, and structures. They reduce the amount of code and improve performance.
Address-of Operator (&)
The address-of operator & returns the address of a variable. However, when displaying the address of a variable, we need to use %u.
#include <stdio.h>
int main() {
int number = 50;
printf("The value of number is: %d, the address of number is: %u", number, &number);
return 0;
}
Output:
The value of number is: 50, the address of number is: fff4
Null Pointer
A pointer that has not been assigned any value but is NULL is called a null pointer. If no address is specified when declaring a pointer, it can be assigned a NULL value, which provides a better method.
int* p = NULL;
In most libraries, the value of a pointer is 0 (zero).
Pointer Program: Swap Two Numbers Without Using a Third Variable
#include <stdio.h>
int main() {
int a = 10, b = 20, *p1 = &a, *p2 = &b;
printf("Before Swap: *p1=%d *p2=%d\n", *p1, *p2);
*p1 = *p1 + *p2;
*p2 = *p1 - *p2;
*p1 = *p1 - *p2;
printf("After Swap: *p1=%d *p2=%d\n", *p1, *p2);
return 0;
}
Output:
Before Swap: *p1=10 *p2=20
After Swap: *p1=20 *p2=10
Reading Complex Pointers
When reading complex pointers in C language, several factors need to be considered. Let’s look at the precedence and associativity of the operators related to pointers.
Operator Precedence Associativity
(), [] 1 from left to right
* Identifier 2 from right to left
Data Type 3 - Here, note that:
-
() : The parenthesis operator is used to declare and define functions.
-
[] : Array subscript operator
-
* : Pointer operator.
-
Identifier : The name of the pointer. Precedence is always given to the identifier.
-
Data Type : The type of variable that the pointer points to. It also includes modifiers such as signed int, long, etc.).
How to Read Pointers: int (*p)[10].
To read a pointer, we must note that () and [] have equal precedence. Therefore, we must consider their associativity. The associativity is from left to right, so precedence belongs to ().
Within the parentheses (), the pointer operator and the pointer name (identifier) p have the same precedence. Therefore, we must consider their associativity, which is from right to left, so precedence belongs to p, followed by.
Assign [] the third precedence because data type has the last precedence. Therefore, the pointer will be as follows:
-
char -> 4
-
* -> 2
-
p -> 1
-
[10] -> 3
The pointer will be interpreted as p is a pointer to an array of size 10 of integers.
Example
How to read the following pointer?
int (*p)(int (*)[2], int (*)(void))
Explanation
This pointer will be interpreted as p is a pointer to a function that has as its first parameter a pointer to a one-dimensional array of integers of size 2, and the second parameter is a pointer to a function that takes void and returns an integer.
Programmer Technical Group
Scan the code to join the group, remember to note: city, nickname, and technical direction.