POINTERS & ARRAYS

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

The pointer in C++ language is a variable, it is also known as locator or indicator that points to an address of a

value.

The symbol of an address is represented by a pointer. In addition to creating and modifying dynamic data
structures, they allow programs to emulate call-by-reference. One of the principal applications of pointers is
iterating through the components of arrays or other data structures. The pointer variable that refers to the same data
type as the variable you're dealing with has the address of that variable set to it (such as an int or string).

Syntax
datatype *var_name;

int *ptr; // ptr can point to an address which holds int data

How to use a pointer?


1. Establish a pointer variable.
2. employing the unary operator (&), which yields the address of the variable, to assign a pointer to a
variable's address.
3. Using the unary operator (*), which gives the variable's value at the address provided by its argument, one
can access the value stored in an address.

Since the data type knows how many bytes the information is held in, we associate it with a reference. The size of
the data type to which a pointer points is added when we increment a pointer.

Advantage of pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving strings, trees etc. and used with
arrays, structures and functions.

2) We can return multiple values from function using pointer.

3) It makes you able to access any memory location in the computer's memory.

Declaring a pointer
The pointer in C++ language can be declared using ∗ (asterisk symbol).

int ∗a; //pointer to int


char ∗c; //pointer to char
Pointer Example
Let's see the simple example of using pointers printing the address and value.

#include <iostream>
using namespace std;
int main()
{
int number=30;
int ∗p;
p=&number;//stores the address of number variable
cout<<"Address of number variable is:"<<&number<<endl;
cout<<"Address of p variable is:"<<p<<endl;
cout<<"Value of p variable is:"<<*p<<endl;
return 0;
}

Output:

Address of number variable is:0x7ffccc8724c4


Address of p variable is:0x7ffccc8724c4
Value of p variable is:30

ARRAYS IN C++
In C++ std::array is a container that encapsulates fixed size arrays. In C++, array index
starts from 0. We can store only fixed set of elements in C++ array.

A collection of related data items stored in adjacent memory places is referred to as an


array in the C/C++ programming language or any other programming language for that
matter. Elements of an array can be accessed arbitrarily using its indices. They can be
used to store a collection of any type of primitive data type, including int, float, double,
char, etc. An array in C/C++ can also store derived data types like structures, pointers, and
other data types, which is an addition. The array representation in a picture is provided
below.
Advantages of C++ Array
o Code Optimization (less code)
o Random Access
o Easy to traverse data
o Easy to manipulate data
o Easy to sort data etc.

Disadvantages of C++ Array


o Fixed size

C++ Single Dimensional Array


Let's see a simple example of C++ array, where we are going to create, initialize and
traverse array.

#include <iostream>
using namespace std;
int main()
{
int arr[5]={10, 0, 20, 0, 30}; //creating and initializing array
//traversing array
for (int i = 0; i < 5; i++)
{
cout<<arr[i]<<"\n";
}
}

Output:

10
0
20
0
30
C++ array with empty members
The maximum number of elements that can be stored in an array in C++ is n. What will
happen, though, if we store fewer than n elements?

For example,

int x[6] = {19, 10, 8}; // store only 3 elements in the array

The array x in this case is 6 elements wide. But we've only given it a three-element
initialization.

When that happens, the compiler fills in the empty spaces with random values. This
random value frequently appears as 0.

Important things to remember while using arrays in C++


1. The array's indexes begin at 0. Meaning that the first item saved at index 0 is x[0].
2. The final element of an array with size n is kept at index (n-1). This example's final element is x[5].
3. An array's elements have sequential addresses. Consider the scenario where x[0beginning ]'s address is
2120.

The address of the subsequent element, x[1], will then be 2124, followed by x[2], 2128, and so forth.

Each element in this case has a four-fold increase in size. This is due to the fact that int has
a 4 byte capacity.

You might also like