
Pointers in C are variables that store the memory location, or memory address, of a data item to be held in memory rather than its value. A memory address is assigned to a pointer. Typically, this address refers to the memory location of some other variable. The first variable points to the second if one variable holds the address of another variable.
When a variable holds the address of the variable’s storage cell, we call it a pointer. The variable declared by the type is also recorded, as is the type specified by the name. The * symbol is used to declare a pointer variable.
Pointers may be valuable, powerful, and relatively painless tools as long as you make sure that they always refer to valid memory in your application.
Table of Contents
Use of Pointers in C
- Memory is stored via pointers.
- Memory information is sent via pointers.
- Because data modification is done using addresses rather than pointers, the execution time is short.
- When a function returns several values, pointers are used.
- To make passing arrays and strings between functions easier.
- For dynamic memory allocation we use pointer.
Declaration of Pointers in C
int *ptr; /* The term *ptr is an int, it says. */
As a more typical, even less significant, and even more straightforward example:
int val = 1; /* An integer */ int *ptr; /* A pointer-to-int */
There are two parts to a pointer variable.
- Pointer operator
- Address operator
*(asterisk) and a variable can be used to represent a pointer operator. The address of an integer datatype is stored in ipr, which is a pointer variable.
int *ptr; /* A pointer-to-int */
A combination of &(ampersand) and a pointer variable is an address operator. The & operator should be used if you wish to return the variable’s memory address. Here, ptr holds the address of a.
ptr = &a;
If we combine the pointer operator and the address operator in one example, the best example of a pointer would be the following:
int a = 10; /* An integer variable */ int *ptr; /* A pointer variable */ ptr = &a; /* Pointer variable holds the address of an integer variable */
#include <stdio.h> int main() { int *ptr, a; a = 10; printf("Address of a: %p\n", &a); printf("Value of a: %d\n\n", a); // 10 ptr = &a; printf("Address of pointer ptr: %d\n", ptr); printf("Content of pointer ptr: %d\n\n", *ptr); // 10 return 0; }
Output: Address of a: 0x7ffc7c5b72bc Value of a: 10 Address of pointer ptr: 2086367932 Content of pointer ptr: 10
In the above program, if you compile and run repeatedly, the output of the address (address of a and ptr) may be different.
Pointer Operators in C
On pointers, we can use the following arithmetic operators.
Operator | Name | Example |
---|---|---|
+ | Addition | *sum = *n1 + *n2 |
– | Subtraction | *sub = *n1 – *n2 |
* | Multiplication | *multi = *n1 * *n2 |
/ | Division | *div= *n1 / *n2 |
% | Modulus | *mod= *n1 % *n2 |
With pointer variables, you may use the increment (++) and decrement (–) operators, but the ++ operator increments the pointer but not by one, and the — operator decrements the pointer but not by one.
According to the data type, the increment and decrement will be done by the number of bytes.
You can also use the following comparison operators to compare two pointer variables:
- Greater than (>)
- Less than (<)
- Greater than or equal to (>=)
- Less than or equal to (<=)
- Equal to (==)
- Not equal to (!=)
What are the various types of pointers in the C language?
The following are the eight categories of pointers:
- Complex Pointer
- Dangling Pointer
- Far Pointer
- Huge Pointer
- Near Pointer
- Null Pointer
- Void Pointer
- Wild Pointer
Null Pointer
When a null value is assigned to a pointer at the moment of declaration, it is nothing but null pointer. What is the value of null pointer? The value of a null pointer is always 0.
#include <stdio.h> int main() { int *p = NULL; // This is the null pointer printf("The value of p is: %d", p); return 0; }
Output: The value of p is: 0
When we don’t want to send any actual memory location in the function parameter, we can use this null pointer. In this scenario, we can pass a null pointer.
Void Pointer
The void pointer has the characteristic of not being associated with any data type. You can cast it to any type and it can hold the address of any type of element.
#include<stdio.h> main() { char ch='A'; int i = 10; void *ptr; ptr = &ch; printf("\n%c", (char *)ch); ptr = &i; printf("\n%d", (int *)i); }
Output: A 10
Array of pointers in C programming
It is also possible to create an array in which several pointers of the same type can be kept.
#include<stdio.h> main() { int *arr[4]; int i, a = 10, b = 20, c =30, d = 40; arr[0] = &a; arr[1] = &b; arr[2] = &c; arr[3] = &d; for( i = 0; i < 4; i++ ) { printf("\n Value is %d ", *(arr[i])); } }
Output: Value is 10 Value is 20 Value is 30 Value is 40
One-Dimensional array with pointer
In a one-dimensional array, a pointer will contain the address of the first element of the array. In the example below the first element is arr[0] and the address of the first element is &arr[0] .
#include<stdio.h> main() { int arr[5] = {10, 20, 30, 40, 50}; int i, *ptr; ptr = &arr[0]; /* Address of first element */ for( i = 0; i < 4; i++ ) { printf("\n Address is %d, Value is %d", ptr, *ptr); ptr = ptr + 1; } }
Output: Address is 1725494832, Value is 10 Address is 1725494836, Value is 20 Address is 1725494840, Value is 30 Address is 1725494844, Value is 40
Two-Dimensional array with pointer
#include<stdio.h> main() { int arr[3][2] = {{10,20}, {30, 40}, {50, 60}}; int i, j, *ptr; ptr = &arr[0][0]; for( i = 0; i < 3; i++ ) { for (j = 0; j < 2; j++) { printf(" %d", *(ptr+ i*2 +j)); } printf("\n"); } }
Output: 10 20 30 40 50 60
Function Pointer in C
The address of a function can be stored in a C program. In C, the idea of storing the address of a function is known as function pointer.
#include<stdio.h> void addition(int n1, int n2){ printf("\n%d", n1 + n2); } void main() { void (*fp) (int a,int b); // This is function pointer fp = addition; //Here pointer stores the function address (*fp) (10,20); // Passing value to the function pointer fp (100,20); // calling function }
Output: 30 120
Pointer to Pointer in C (Double Pointer)
In C programming we can create a pointer which will hold the address of another pointer. It is also called double pointer.
int var = 10; int *ptr1; int **ptr2; ptr = &var; ptr = &ptr1;
#include<stdio.h> void main() { int var = 10; int *ptr1; int **ptr2; ptr1 = &var; ptr2 = &ptr1; printf("\n%d",var); printf("\n%d",*ptr1); printf("\n%d",**ptr2); }
Output: 10 10 10
In conclusion, In this article we have covered the basics of pointer in C programming and pointer types, examples using pointer, pointer array, function pointer and pointer to pointer.
Hope you liked this article and this article can help you in the example and interview.