/* */ Click to Join Live Class with Shankar sir Call 9798158723

Pointer to array in C++ ?


A pointer variable can store address of other variable . Not only can a pointer store the address of a single variable, it can also store the address of cells of an array.

example

Example:-

int arr[5];
int *p;
p=arr;
Here, p is a pointer variable, and arr is an array. When code p=arr will be excuted p stores the address of the first elements of array in variable p; We can write, p=arr or p= &arr[0] both are same.
Q) W.A.P to initialize pointer to array.
// C++ program to illustrate the concepts
// of creating 1D array of pointers
#include < iostream>
using namespace std;

// Driver Code
int main()
{
	// Dynamically creating the array
	// of size = 5
	int* p = new int[5];

	// Initialize the array p[] as
	// {10, 20, 30, 40, 50}
	for (int i = 0; i < 5; i++) {
		p[i] = 10 * (i + 1);
	}

	// Print the values using pointers
	cout << *p << endl;
	cout << *p + 1 << endl;
	cout << *(p + 1) << endl;
	cout << 2 [p] << endl;
	cout << p[2] << endl;
	*p++;

	// Pointing to next location
	cout << *p;

	return 0;
}