
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Applications of Pointers in C/C++
To access array elements
We can access array elements by using pointers.
In C
Example
#include <stdio.h> int main() { int a[] = { 60, 70, 20, 40 }; printf("%d\n", *(a + 1)); return 0; }
Output
70
In C++
Example
#include <iostream> using namespace std; int main() { int a[] = { 60, 70, 20, 40 }; cout<<*(a + 1); return 0; }
Output
70
Dynamic Memory Allocation
To dynamically allocate memory we use pointers.
In C
Example
#include <stdio.h> #include <stdlib.h> int main() { int i, *ptr; ptr = (int*) malloc(3 * sizeof(int)); if(ptr == NULL) { printf("Error! memory not allocated."); exit(0); } *(ptr+0)=1; *(ptr+1)=2; *(ptr+2)=3; printf("Elements are:"); for(i = 0; i < 3; i++) { printf("%d ", *(ptr + i)); } free(ptr); return 0; }
Output
Elements are:1 2 3
In C++
Example
#include <iostream> #include <stdlib.h> using namespace std; int main() { int i, *ptr; ptr = (int*) malloc(3 * sizeof(int)); if(ptr == NULL) { cout<<"Error! memory not allocated."; exit(0); } *(ptr+0)=1; *(ptr+1)=2; *(ptr+2)=3; cout<<"Elements are:"; for(i = 0; i < 3; i++) { cout<< *(ptr + i); } free(ptr); return 0; }
Output
Elements are:1 2 3
Passing arguments to a function as a reference
We can use pointers to pass arguments by reference in functions to increase efficiency.
In C
Example
#include <stdio.h> void swap(int* a, int* b) { int t= *a; *a= *b; *b = t; } int main() { int m = 7, n= 6; swap(&m, &n); printf("%d %d\n", m, n); return 0; }
Output
6 7
In C++
Example
#include <iostream> using namespace std; void swap(int* a, int* b) { int t= *a; *a= *b; *b = t; } int main() { int m = 7, n= 6; swap(&m, &n); cout<< m<<n; return 0; }
Output
67
To implement data structures like linked list, tree we can use pointers also.
- Related Articles
- Pointers, smart pointers and shared pointers in C++
- Pointers in C/C++
- Pointers vs References in C++
- What are pointers in C#?
- Explain the concept of pointers in C language
- Explain array of pointers in C programming language
- What are Wild Pointers in C/C++?
- How to compare pointers in C/C++?
- RAII and smart pointers in C++
- How many levels of pointers can we have in C/C++?
- near, far and huge pointers in C
- Difference between Array and Pointers in C
- Demonstrate the concept of pointers using C language
- Explain the concepts of Pointers and arrays in C language
- What are the different types of pointers in C language?

Advertisements