Categories: C++Learn C++

Pointer and one dimensional array in C++

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;
}

 

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.