Relation between pointers and array in C++


relation between array and pointers,passing array to a function,returning array from a function

Array and pointers are related closely to one another.The array syntax is just another way to use the pointers .To understand what this means we will have to look at how compiler interpret an array.

Frankly speaking the compiler does not know what an array is.Whenever it come across an array syntax:a variable name with a subscript say arr[2] ,it will interpret this syntax to the pointer form.So arr[2] is interpreted as *(arr+2).Looking at this form the compiler understand which address to access-the address of the storage 2 times away from arr address- and output the value accordingly.

So whenever you use array we can directly use the pointer form instead of the subscript syntax to access the value.Consider the code below.

Code example

int arr[]={ 45 , 90 , 536 };

cout<< *(arr+1) << endl
<< arr[1] << endl ;

Output

90 90





Assigning array to pointer

You can use pointer to access the array,but to do this the first address of the array must be assigned to the pointer.Once the pointer is assigned the first address you can access all the values using the pointer by incrementing the address by adding a value.

In any array the name of the array -without the subscript- gives the first address of the array.For instance ina an array arr[4] , the name ‘arr’ gives the address of the first storage in the array.You can also simply add an ‘&'(ampersand) in front of the first array element to get the address.

Code example

int arr[]={ 12 , 45 , 59 };

int *arrPt=arr; ///work fine
// or arrPt=&arr[0] ///work fine

/*Accessing all the array elements */
for( int i=0 ; i<sizeof(arr)/sizeof(int) ; i++ )
{
cout<< *(arrPt+1) << ” ” ;
}

Output

12 45 59

Once the pointer is assigned the first address of the array we can also use the subscript syntax to access the array.This is another proof of how closely the array and pointer are related; pointer behave like an array.

Note:include <cstring> to use the strlen() function.This function gives the number of characters in a string.

Code example

char str[]=”New string” ;

char *cPt=str ;

for(int i=0; i<strlen(str) ; i++ )
{
cout<< cPt[i] << ” ” ; ///work fine
}

Output

New string


 


Stop wasting time,earn money($$$) from your website-Join Now!

Passing array to the function

To pass an array to a function we need not pass the whole elements of the array one by one,instead we can just pass the first address of the array.In the function to receive the array we can either use a pointer or the usual array format .

If the array format is used we may or may not mentioned the size of the array,either way it works fine.But it is better to mentioned the size else we must pass another argument as the size of the array.In the program below we will pass an array to a function and receive it using a pointer and a [] syntax format.

#include <iostream>

using namespace std ;

//Receiving array with pointer and [] syntax
void func(float *fp , float fa[] , size_t sz )
{
for(int i=0 ; i < sz ; i++ )
{
cout << fp[i] << endl ;
}

cout<< “\n\n” ;

/* Accessing through [] syntax */
for(int i=0 ; i < sz ; i++ )
{
cout << fa[i] << endl ;
}

cin.get();
return 0 ;
}

int main()
{
float f[5] = { 2E4 , 3.23E1 , 67.90 , 12.789 , 666.666 } ;
func( f , f , 5 ) ;

cin.get( );
return 0 ;
}

 

 






Returning array from the function

Passing an array to a function requires only passing the first address of the array.Well obviously so we also return an array from function by passing back only the first address of the array. A program is given below demonstrating the concept of returning an array from a function as a pointer.

#include <iostream>

using namespace std ;

///A function returning an array
int *func( int arr[] ,size_t sz )
{
for(int i=0;i< sz ; i++)
{
arr[i]=arr[i]*i ;
}

return i ;
}

int main( )
{
int arr[]={ 23 ,34 , 56 , 89 };
int *ii=func( arr, 4 ) ;

///Array returned by function
for( int i=0 ; i<3 ; ++i )
{
cout<< ii[i] << endl ; /// or cout<< *(ii + i) << endl ; work fine
}

cin.get( ) ;
return 0 ;
}

Output

0 34 112 267


*Side Note

If you use user-defined type:class or structure, instead of the built-in type,you will realize that there is no need for passing or returning array from function.Once the array is declared as part of the type,it is accessible from anywhere within function.But note the function must be also declared as member function of the type.


 


Stop wasting time,earn money($$$) from your website-Join Now!