Tech Study

C++ Pointer and Array (with Examples)

 C++ Pointer And Array

Arrays

Arrays store multiple values of the same data type in a single variable, instead of using different variables for each value.

How to declare an array in C++

Syntax: data_type array_name[array_size]

Ex:

int arr[10];

here int is data type

arr is array name

10 is the array size

Here we declare an array is size 10 which contains all elements of int data type.

Indexing in arrays

Each element in an array is associated with a number known as index through which they can be accessed. Arrays index starts with 0 index  and end with array_size -1 index.

Arrays follow 0-based indexing. 

How to initialize an array in c++

We can declare the array during initialization in the following ways.

int arr[5]={1,2,3,4,5};

int arr[]={1,2,3,4,5};

If we only declare the array and do not initialize it (like int arr[10];), the array stores garbage values by default.In some compilers , that garbage value is 0.Here, all the 10 elements of the array will be garbage values or 0’s by default.

Display array elements

We can display the elements of the array by iterating the array elements using a loop.

Example:

#include <iostream>

using namespace std;




int main() {




  int numbers[5] = {7, 5, 6, 12, 35};




  cout << "The numbers are: ";




  //  Printing array elements

  for (const int &n : numbers) {

    cout << n << "  ";

  }




  cout << "\nThe numbers are: ";




  //  Printing array elements

  for (int i = 0; i < 5; ++i) {

    cout << numbers[i] << "  ";

  }




  return 0;

}

Output:

The numbers are: 7  5  6  12  35 

The numbers are: 7  5  6  12  35

Pointers

C++  pointers are special kinds of variables that instead of containing data, contain addresses of other variables. A pointer can store the address of a single variable(single cell), or multiple cells like an array, string, vector, etc..

Pointer variables contain the addresses of other basic variables.

Pointers can store the addresses of all types of variables which means they can store the addresses of pointer variables also.

 In C++, pointers have great significance as they are used in many data structures like linked lists, trees etc.

”*” indicates that the following variable is a pointer.

Dereferencing a pointer

We use a * symbol to access the value of a variable that a pointer is pointing to.

Ex:

int * p;

int var=10;

p=&var;

cout<<*p<<endl;

Points and Arrays

The name of an array is also considered a pointer, i.e., the name of an array contains the address of an element. C++ considers the =name of the array as the address of the first element of the array.

// Code 

int *p; 

int arr[6]; 

// store the address of the first element of arr in ptr

 p = arr;

Here, p is a pointer variable and arr is an integer array.p=arr  means the pointer variable p stores the address of the first element of arr.

Writing arr instead of arr[0]  means the same to the compiler. So we can write the code in the below-mentioned way also.

int *p; 

int arr[6];

 p = &arr[0];

similarly, we can refer to the remaining elements of the array by arr[1], arr[2], arr[3], arr[4], and arr[5].

We can point to every element of the array using the same pointer. If p points to the first element of the array, then p+4 points to the fifth element of the array.

int *p; 

int arr[6]; 

p = arr;

Here,

p + 1 is equivalent to &arr[1]

p + 2 is equivalent to &arr[2]

 p + 3 is equivalent to &arr[3] 

p + 4 is equivalent to &arr[4]

p + 5 is equivalent to &arr[5]

similarly, we can access all the elements of the array using a single pointer.

*(p+1) is equivalent to arr[1]

*(p+2) is equivalent to arr[2]

*(p+3) is equivalent to arr[3]

*(p+4) is equivalent to arr[4]

*(p+5) is equivalent to arr[5]

The address difference between p and p+1 here differs by 4 bytes as p is pointing to integer data and the size of int is 4 bytes in a 64-bit system. If the pointer points to the char data type, then the address difference between p and p+1 is 1 byte.

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more