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

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.