综合编程

Understanding Pointer to Array in C++

微信扫一扫,分享到朋友圈

Understanding Pointer to Array in C++

Table of Contents

  • 2
    Pointer Arithmetics
  • 3
    Pointers And Arrays in C++
  • 4
    Passing An Array to Function Using Pointers

Introduction

Today in this tutorial, we are going to understand the concept of the Pointer to Array in C++
.

Before we head directly to the topic, we first would like to guide the readers through the pointer arithmetics in C++. It is going to help understand the concept in a better way.

Pointer Arithmetics

Basically,pointers are variables that store the address of any other variable of a specific data type.

Let us go through a simple example to get a clear idea about the pointer arithmetics.

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int  i = 3, *x;
float  j = 1.5, *y;
cout<<"Value of i= "<<i;
cout<<"nValue of j= "<<j;
x = &i ;
y = &j ;
//Original pointer values
printf("nOriginal address in x = %u",x);
printf("nOriginal address in y = %u",y);
//Increasing pointer addresses
x++ ;
y++ ;
//After increment
printf("nNew address in x = %u",x);
printf("nNew address in y = %u",y);
cout<<"nnSize of int for this compiler = "<<sizeof(int);
cout<<"nSize of float for this compiler = "<<sizeof(float);
return 0;
}

Output:

Value of i= 3
Value of j= 1.5
Original address in x = 7339516
Original address in y = 7339512
New address in x = 7339520
New address in y = 7339516
Size of int for this compiler = 4
Size of float for this compiler = 4

Here,

  • We first initialize two int
    and float
    variables i and j respectively with some values,
  • Then we store the address of the two variables inside two pointers x
    and y
    in lines 11
    & 12
    , and print them out,
  • After that, we increment both the pointers and see the change in output.

Observe the 5th
and 6th
lines of the output. 7339520
is original value in x plus 4, and 7339516
is original value in y plus 4. This so happens because every time a pointer is incremented it points to the immediately next location of its type.

That is why, when the integer pointer x
is incremented, it points to an address two locations after the current location since an integer is always 4 bytes long (We checked that using the sizeof() function above).

Similarly, y
points to an address 4 locations after the current location. This is a very important result and can be effectively used while passing the entire array to a function.

Note:Do not attempt the following operations on pointers, they would never work,

  • Addition of two pointers,
  • Multiplication of a pointer with a constant,
  • Division of a pointer with a constant.

Pointers And Arrays in C++

Now, let us take a deep look at accessing array elements using pointers.

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int arr[4]= { 10, 20, 30, 40 };
int *ptr;
ptr=&arr[0]; //same as ptr=arr(arr is the base address)
//printing address and values of array
//elements using pointers
for(int i=0; i<4; i++)
{
printf("nAddress = %u",(ptr+i));
cout<<"tValue = "<<*(ptr+i);
}
return 0;
}

Output:

Address = 7339504       Value = 10
Address = 7339508       Value = 20
Address = 7339512       Value = 30
Address = 7339516       Value = 40

In the code above,

  • At first, we initialize an array of size 4
    and a pointer for the same type,
  • After that, we assign the base address or the address of the 1st element of the array to the pointer(Since, arr
    and &arr[0]
    are same), ptr
    ,
  • Then we print out the elements and their corresponding addresses using pointers.

As we discussed in the previous section, (ptr+i)
is the address of the ith element from the array arr
. And *(ptr+i)
actually refers to the value at the address (ptr+i)
. Therefore, it gives us the value of the ith element.

Passing An Array to Function Using Pointers

Let us see how we can pass a whole array to a function with the help of pointers as an example, to get a clear understanding of the topic.

#include<iostream>
using namespace std;
void show(int *ptr, int n)
{
//printing the whole array
for(int i=0; i<n; i++)
{
cout<<"arr["<<i<<"] = "<<*(ptr+i)<<endl;
}
}
int main()
{
//Array initialisation
int arr[5]= { 12, 45, 86, 73, 87 };
show( arr, 5);
//function call with base address(arr) and no.of elements(6)
return 0;
}

Output:

arr[0] = 12
arr[1] = 45
arr[2] = 86
arr[3] = 73
arr[4] = 87

Similar to the previous example, here we initialize an array inside the main()
and pass the base address arr
and the array size
as parameters to the show()
function we defined before.

After the call of the show() function, at this point, the pointer ptr
stores the base address of the array(arr) and n
is the size. Further, we use a for
loop to print the whole array using the concept of a pointer to array.

Conclusion

So, in this tutorial, we learned the working as well as the use of the Pointers to Arrays in C++
. Practicing the above codes/examples would help understand the topic further.

For any questions, feel free to use the comments below.

References

Virtual Background For Video Conferencing In Python and OpenCV - A Silly Approach

上一篇

Being a JS Developer, this is What Keeps Me Up at Night.

下一篇

你也可能喜欢

评论已经被关闭。

插入图片

热门栏目

Understanding Pointer to Array in C++

长按储存图像,分享给朋友