Learn about pointers

Akash Srivastava
Sep 6, 2018 · 5 min read

original post : https://cppcompetitive.blogspot.com/2018/07/pointers.html

Hello again Coders, Today we will move on to one of important concept of programming called Pointers. As in the storage of your PC we have certain data stored and for every memory allocation we have a address in hexadecimal format and by just pointing to these addresses we can access the memory allocation directly at any place in the program.

Pointer definition:The variable that stores the address of another variable is what in C++ is called a pointer.As from definition we can say that a pointer is a variable which stores the memory address of another variable.

syntax:
int *a;

//for pointing to a variable
int b=5;
int *a=&b; //method 1
int *c; //method 2
c=&b;

In both of the methods we can store memory address to the pointer.The symbol “*” is also known as deference operator.
**while declaring any pointer we declare its data type as same of the variable we want to point. so if we want to use pointer on character variable we use:
char *a;
as a pointer only stores a hexadecimal value the storage size depends upon your PC, if your PC is 64 bit operating then the size of storage of pointer will be 8-byte.

“&” this operator is used to grab the address of the variable stored. We used this as int *a=&b; because we want to store address of b into pointer a. So by operating it “&” we grabbed the address of the variable b and stored it into a.

now lets perform some operations on pointer and see what is its potential.

#include<iostream>
using namespace std;
int main(){
int a=15;
int *b=&a;
cout<<b<<endl;
cout<<*b;
return 0;
}

output:

0x7fff9fbb65ac

15

so lets see the code, we declared a integer type variable of value 15 and then we allocated the address of a to the pointer of name b. At this point we have this.

As, we wrote cout<<b; , so the address stored in b got printed. On next line we deferred the address by using “*” operator to the memory “a” and the output we got is 15, So that’s how it works.

Now, if we do &a the output will be the address of a.

Well lets cover some more on pointers.

Pointers In Arrays:

Array is a continuous memory allocation so the address stored for each cells of any data type should be continuous like House’s address on a street. And that’s how we can access each elements using their address and traversing through them one by one, likewise you search for any house by its address. Now see how to access a array using pointer.

int arr[5]={1,2,3,4,5};
int *ptr=&arr[0];

so here we just stored the address of the first element of the array in a pointer.

**Array name, as in example “arr” provides us the address of 1st element automatically so we can do the below operation also:

int arr[5]={1,2,3,4,5};
int *ptr=arr;

For accessing the next element, we just have to use ptr++ as because ptr stores a address and by applying increment operator we jumped over to next address as because the storage is in linear form.

** as we declared integer pointer it will automatically jump over to 4-bytes to the next element.

see the diagram:

as in the diagram we have our first array as character array (char size 1-byte) , second one is short int array (short size 2-bytes) third is long one (long size 4-bytes)…….

Now lets try some questions on Arrays:

Ques 1: Take a input N and array of size N, now display all the element using pointers.

Constraint: 0<=N<=100

Sample Input: 5

1 2 3 4 5

Sample Output: 1 2 3 4 5

Code:

#include<iostream>
using namespace std;
int main(){
int N,arr[100];
cin>>N;
int *ptr=arr; //initialising pointer
for(int i=0;i<N;i++){ //for taking input
cin>>*ptr;
ptr++;
}
ptr=arr; //initialising pointer again as the pointer was not at initial point
for(int i=0;i<N;i++){ //fot output
cout<<*ptr<<" ";
ptr++;
}
return 0;
}

So, What we have done is only we travelled through the addresses of each element and outputted all the value stored on that address.

Ques 2: Take Input number N and array of size N. and reverse the order of the digits using pointers.

Constraint: 0<=N<=100

Sample Input: 5

1 2 3 4 5

Sample Output: 5 4 3 2 1

Code:

#include<iostream>
using namespace std;
int main(){
int N;
cin>>N;
int arr[100];
for(int i=0;i<N;i++){
cin>>arr[i];
}
int *ptrF=arr;
int *ptrB=&arr[N-1];
int i=0;
while(i<(N+1)/2){
//swap(*ptrF,*ptrB); //you can use swap function as inbuilt function
//but for now we will do this
int temp=*ptrF;
*ptrF=*ptrB;
*ptrB=temp;
ptrF++;
ptrB--;
i++;
}
for(int i=0;i<N;i++){
cout<<arr[i]<<" ";
}
return 0;
}

lets understand the code :

int *ptrF=arr;                          //the main section
int *ptrB=&arr[N-1];
int i=0;
while(i<(N+1)/2){
//swap(*ptrF,*ptrB); //you can use swap function as inbuilt function
//but for now we will do this
int temp=*ptrF;
*ptrF=*ptrB;
*ptrB=temp;
ptrF++;
ptrB--;
i++;
}

So this is the main part of our program, and in this at first we saved the address of first element of array inside ptrF and on next line we stored the address of last element of the array inside *ptrB.

We used while to travel along array. Condition is : till i is less than the middle element. Now inside while loop we have to the swap the first-latest element to the latest-last element for that we used a temporary variable so the the value of *ptrF doesn’t get lost when we equate *ptrF to *ptrB. After the value got swapped we incremented the ptrF to the next element, and the ptrB decremented to previous one.

That’s all for now, Today we have done very less amount of questions, But you will see its real application in functions and linked lists related post. So, practice basic questions using pointers and try to do all array related questions using pointers. We will cover types of sorting in upcoming post. Please follow this blog and support the content.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade