Pointers to an array


Pointers to an array

It's very possible that you won't comprehend this chapter unless you've gone through the C++ Pointers chapter.

So, assuming you know your way around pointers in C++, let's get started: A constant pointer to the array's first element is an array name. As a result, in the statement, there is a double balance[50];

balance is a pointer to &balance[0], the address of the array balance's first entry. As a result, the following software snippet assigns p the address of balance's first element: double *p; double balance[10];

p = equilibrium;

Constant pointers can be used as array names, and vice versa. As a result, *(balance + 4) is a valid method of gaining access to the data at balance[4].

After storing the first element's address in p, you can access array elements with *p, *(p+1), *(p+2), and so on. The following is an example that demonstrates all of the principles stated above.

Example:

#include <iostream> using namespace std; int main() { int arr[5] = { 0, 6, 53, 98, 2 }; int *ptr = arr; cout<<"\n by ponter \t:"; for(int i=0;i<5;i++) { cout<< *ptr++<<" "; } cout<<"\n by array intex :"; for(int j=0;j<5;j++) { //call by ponter cout << arr[j]++<<" "; } return 0; }

OUTPUT:

string type