Arrays and pointers are common in most programming languages. Most importantly, these two data structures are needed everywhere from any moderate to complex program. Here we will try to know details about these two data structures. Arrays and pointers are almost the same except for a few minor differences.

Array

Let’s say we want to add the numbers from 1 to 10; So what should we do? We can take ten variables and add them one by one. What if we write this program in C or C++?

/******************************************************************************
 
Author: Shahinur
Description: This program is to add number from 1 to 10 without using array
 
*******************************************************************************/
 
#include <iostream>
using namespace std;
 
int main() {
 int sum=0;
 int a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10;
  
 sum=a+b+c+d+e+f+g+h+i+j;
  
 cout<<sum;
  
 return 0;
}

Maybe this program does not seem difficult to anyone. But what if we want to add from 1 to 100 or 1000? Should we declare that number of variables? Yes, we can; But that is never rational. And so, the origin of arrays is to overcome this problem.
That is, an array is a collection of data of the same type. When we need to use many variables of the same data type, we use arrays. An array has a specific index number through which a specific element of that array is indicated. Let a[0] be an array whose number of elements is 10, then a[0] will point to the first element of this array. Here it is important to remember that array index always starts from 0. If we do the above program with an array then it will be –

/******************************************************************************
 
Author: Shahinur
Description: This program is to add number from 1 to 10 using array
 
*******************************************************************************/
 
#include <iostream>
using namespace std;
 
int main() {
 int sum=0;
 int a[10];
 a[0]=1,a[1]=2,a[2]=3,a[3]=4,a[4]=5,a[5]=6,a[6]=7,a[7]=8,a[8]=9,a[9]=10;
  
 sum=a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9];
  
 cout<<sum;
  
 return 0;
}

Does it seem more complicated? Indeed it is. It is more troublesome than before. But the main advantage here is the use of the indexes. Using this index we can use loops. And using this loop we can do many complex tasks very easily. To understand that we see the following program –

/******************************************************************************
 
Author: Shahinur
Description: This program is to add number from 1 to 10 using array
 
*******************************************************************************/
#include <iostream>
using namespace std;
 
int main() {
    int sum=0;
    int a[10];
        for(int i=0;i<10;i++)
          {
           cin>>a[i];
          }
     
    for(int i=0;i<10;i++)
           {
           sum+=a[i];
           }
     
    cout<<sum;
     
    return 0;
}

The fun part of this program is that there is no number requirement. Even if I want to add up to 100 or 1000, there is no need to change the program.

Arrays have various complex applications, but we won’t go into that now.

Pointer

A pointer is an object that points to the address of another variable in computer memory. All the variables we use are stored in a specific place in the computer’s memory. The array we saw earlier is also stored at some address.

Pointers are generally more efficient than arrays. When we declare the size while declaring an array, the array occupies that amount of space in memory; Even if we don’t use that array anywhere, still. But this is not the case with pointers. The pointer occupies only as much space as it needs. Another thing is that arrays are arranged sequentially, i.e. one after the other. But pointers may or may not.

Now lets see an example using pointers –

/******************************************************************************
 
Author: Shahinur
Description: This Program demonstrate the pointer in C++
 
*******************************************************************************/
#include <iostream>
using namespace std;
 
int main()
{
    int val1=5, val2=10;
    int *p1, *p2;
     
    p1=&val1;
    p2=p1;
    cout<<p1<<endl<<*p2<<endl<<p2<<endl<<*p1;
 
    return 0;
}

In my case the output is –

0x7fff4d0af138
5
0x7fff4d0af138
5

Here we can see that once an address is printed, and another time a value is printed which we put in val1. And later the same thing happened again. Basically the address shown here is the address where the value ‘5’ is stored. The value shown in my case will not show in your case, a different address will show. What is the reason? This is because memory occupies one place at a time. Although we see the same value over and over for a variable, different things actually happen in computer memory. A lot of fun, isn’t it?

Now let’s discuss why our output is like this –

* symbol is used to declare a pointer variable. We have declared two pointers named p1 and p2 in line number 12.

Then I assigned the address of val1 to pointer p1. It is better to say here that to access the address of a variable, the ‘&’ sign must be given before it. If we had not given the & sign in line 14, our program would have failed. Why was it wrong? Think about it and see if you can figure it out? Well, I’ll tell you. Here pointer p1 is an address, and variable val1 is a value. And in programming, comparison or mathematical operations can always be done on the same data type, which is not the case with different types.

In the next line we assign p1 to p2. Here the normal assignment operator is used. Because they are both of the same type.

Later when we printed p1 it printed an address, again when we printed *p1 it printed the value that was in the address of p1. That is, if we want to print the value of a pointer, we have to give * this star symbol.

Similarly since we assigned p1 to p2, all values of p1 are copied to p2. I hope you can figure out the rest yourself.

If you have any comments or suggestions about this, then definitely share your opinion below the post.

Thanks 🙂

0 0 votes
Article Rating
0
Would love your thoughts, please comment.x
()
x