Prerequisite : Pointers in C/C++, Memory Layout of C Programs.
- To pass arguments by reference. Passing by reference serves two purposes
(i) To modify variable of function in other. Example to swap two variables;
C
#include <stdio.h>
void swap( int * x, int * y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int main()
{
int x = 10, y = 20;
swap(&x, &y);
printf ( "%d %d\n" , x, y);
return 0;
}
|
C++
#include <iostream>
using namespace std;
void swap( int * x, int * y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int main()
{
int x = 10, y = 20;
swap(&x, &y);
cout << x << " " << y << endl;
return 0;
}
|
Output :
20 10
(ii) For efficiency purpose. Example passing large structure without reference would create a copy of the structure (hence wastage of space).
Note : The above two can also be achieved through References in C++.
- For accessing array elements. Compiler internally uses pointers to access array elements.
C
#include <stdio.h>
int main()
{
int arr[] = { 100, 200, 300, 400 };
printf ( "%d " , arr[2]);
printf ( "%d\n" , *(arr + 2));
return 0;
}
|
C++
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 100, 200, 300, 400 };
cout << arr[2] << " " ;
cout << *(arr + 2) << " " ;
return 0;
}
|
Output :
300 300
- To return multiple values. Example returning square and square root of numbers.
C
#include <math.h>
#include <stdio.h>
void fun( int n, int * square, double * sq_root)
{
*square = n * n;
*sq_root = sqrt (n);
}
int main()
{
int n = 100;
int sq;
double sq_root;
fun(n, &sq, &sq_root);
printf ( "%d %f\n" , sq, sq_root);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
void fun( int n, int * square, double * sq_root)
{
*square = n * n;
*sq_root = sqrt (n);
}
int main()
{
int n = 100;
int * sq = new int ;
double * sq_root = new double ;
fun(n, sq, sq_root);
cout << *sq << " " << *sq_root;
return 0;
}
|
Output :
10000 10
- Dynamic memory allocation : We can use pointers to dynamically allocate memory. The advantage of dynamically allocated memory is, it is not deleted until we explicitly delete it.
C
#include <stdio.h>
#include <stdlib.h>
int * createArr( int n)
{
int * arr = ( int *)( malloc (n * sizeof ( int )));
return arr;
}
int main()
{
int * pt = createArr(10);
return 0;
}
|
C++
#include <iostream>
using namespace std;
int * createArr( int n)
{
return new int [n];
}
int main()
{
int * pt = createArr(10);
return 0;
}
|
Some Questions Regarding Pointers:
- What are the uses of a pointer?
Ans. Pointer is used in the following cases
i) It is used to access array elements
ii) It is used for dynamic memory allocation.
iii) It is used in Call by reference
iv) It is used in data structures like trees, graph, linked list etc.
- Are pointers integer?
Ans. No, pointers are not integers. A pointer is an address and a positive number.
- What does the error ‘Null Pointer Assignment’ means and what causes this error?
Ans. As null pointer points to nothing so accessing a uninitialized pointer or invalid location may cause an error.
- How pointer variables are initialized?
Ans. Pointer variables are initialized by one of the following ways.
I. Static memory allocation
II. Dynamic memory allocation
- What is pointer to a pointer?
Ans. If a pointer variable points another pointer value. Such a situation is known as a pointer to a
pointer.
Example:
int *p1,**p2,v=10;
P1=&v; p2=&p1;
Here p2 is a pointer to a pointer
- What is an array of pointers?
Ans: If the elements of an array are addresses, such an array is called an array of pointers.
- To implement data structures.
Example linked list, tree, etc. We cannot use C++ references to implement these data structures because references are fixed to a location (For example, we can not traverse a linked list using references)
- To do system level programming where memory addresses are useful. For example shared memory used by multiple threads. For more examples, see IPC through shared memory, Socket Programming in C/C++, etc
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
25 May, 2022
Like Article
Save Article