Table of Contents
Pointer and one dimensional array in C++
In this tutorial, we will try to understand Pointer and one dimensional array in C++.
First, we will look at array.
What is the array?
Array is a data type in many programming languages and array is one of structured data types.
Array contains multiple elements.
Elements are organised contiguously in memory.
Array contains homogeneous elements therefore array always contains the same data type.
What is a pointer?
Pointer is a variable that stores another variable’s memory address.
Here is an example of one dimensional array in C++. We must understand array structure before learn this chapter in C++
program 1
#include <iostream>
using namespace std;
int main()
{
int marks[]{34,56,75,68,97,43,24,52,47,87
};
cout << “List of marks” << endl;
cout << “\nmarks 1: ” << marks[0];
cout << “\nmarks 2: ” << marks[1];
cout << “\nmarks 3: ” << marks[2];
cout << “\nmarks 4: ” << marks[3];
cout << “\nmarks 5: ” << marks[4];
cout << “\nmarks 6: ” << marks[5];
cout << “\nmarks 7: ” << marks[6];
cout << “\nmarks 8: ” << marks[7];
cout << “\nmarks 9: ” << marks[8];
cout << “\nmarks 10:” << marks[9];
return 0;
}
In this program, we have displayed the element of one d array in C++. In this case,10 elements of an array are displayed.
Array with pointer
How are pointers and arrays related?
arrayname=point to the first element in the array
&arrayname=point to the address of first element in the array
&arrayname[0]=point to the first element in the array
Program 2
#include <iostream>
using namespace std;
int main()
{
int marks[]{34,56,75,68,97,43,24,52,47,87};
cout <<“address of marks: “<<marks<< endl;
cout <<“address of marks: “<<&marks<< endl;
cout <<“address of marks: “<<&marks[0]<< endl;
return 0;
}