www.openskytechnology.comDescription: Description: D:\website\syllabus\study_matreials\c++\logo_pic\logo.jpg

Training Home

Reference to an Array

  _______________________________________________________________________

 

/* The following function demostrates the implementations of the "refernce to

  an array of one dimension" and the "pointer to an array of 2 dimension"

  Reference to an array:

    int arr[10] = {1,2,3,4,5,6,7,8,9,0};  // 1 D array of 10 integers

    int (&ref_arr)[10] = arr; //reference to an array. Note: array is one dimensional

    int arr2D[2][3] = {

                        1,2,3,      //2 D array

                        3,4,6

                      };

    int (*ptr_arr2D)[3] = arr2D; //Pointer to an array. Note" Array is 2 dimentional

    int& Display(int (&rArr)[10], int (*ptrArr)[3]) //function with two parameters

    int&  (*fptr)(int(&)[10], int (*)[3])= Display;  //pointer to a function containing a reference to an array and a pointer to an array and return int reference

  */

#include "stdio.h"

typedef int pointerToArray [16];

int value=100;

int oneD[16];

int& Display(int (&rArr)[10], int (*ptrArr)[3])

{

   static int temp[16];

   int i=0;

   printf("Displaying the one dimentional array: \n");

   int count=0, counter=0;

   for (count=0; count < 10; ++count)

   {

      temp[i++]=rArr[count];

      printf("%d ", rArr[count]);

   }

   printf("\n Prining the 2D array\n");

   for (count=0; count<2; ++count)

   {

        for (counter=0; counter<3; ++counter)

        {

            printf("%d ", ptrArr[count][counter]);

            temp[i++]= ptrArr[count][counter]++;

        }

        printf("\n");

   }

//   return ptrArr;

   return value;

}

void Displayi(int* ptrArr)

{

   for (int count=0; count<16; ++count)

   {

        printf("%d ", ptrArr[count]);

   }

}

int main()

{

    int arr[10] = {1,2,3,4,5,6,7,8,9,0};  //an array

    int (&ref_arr)[10] = arr; //reference to an array. Note: array is one dimensional

    int arr2D[2][3] = {

                        1,2,3,

                        3,4,6

                      };

    int (*ptr_arr2D)[3] = arr2D; //Pointer to an array. Note" Array is 2 dimentional

    int&  (*fptr)(int(&)[10], int (*)[3])= Display;

    int* array;

    fptr(arr, arr2D) = 500;

    printf("value = %d\n", value);

//    ptr_arr2D= fptr(arr, arr2D);

//    Display(array);

}

_______________________________________________________________________

 

 

Copyright © Open Sky Technology

 

Corporate Office:  #4, RR Complex, 2nd Floor, Munnekolala, Marathahalli, Bangalore – 560037. (Landmark: near Munnekolala Bus Stop)   Mobile: 9886333765,