Reference to an array in C++

You can read here to learn more about reference, and this post focuses only on the reference to an array.

We already learnt that without specifying the size, reference to an array can't be defined. Reference to an array should be bound with known size.  


What if the reference to an array subscript doesn't hold value?

Program 1 

#include <iostream>

// A function to implement the sorting
void doSomething(int (&arr)[], int n)
{
    int i, j;
    for (i = 0; i < n - 1; i++) {
	for (j = 0; j < n - i - 1; j++) {
	    if (arr[j] > arr[j + 1]) {
		std::swap(arr[j], arr[j + 1]);
	    }
	}
    }
    return;
}

// Function to print an array
void printArray(int arr[], int size)
{
     int i;
     for (i = 0; i < size; i++)
	std::cout << "i=" << i << "   " << arr[i] << "   ";
	std::cout << std::endl;
}


// Driver code
int main()
{
    int arr[] = { 5, 1, 4, 2, 8};
    int N = sizeof(arr) / sizeof(arr[0]);

    doSomething(arr, N);

    std::cout << "Sorted Original array: \n";
    printArray(arr, N);

    return 0;
}

Output:

1
2
Line 4: error: parameter 'arr' includes reference to array of unknown bound 'int []'
compilation terminated due to -Wfatal-errors.

Click here to debug the above program.

Here, in the first argument of doSomething, we failed to specify the size which is part of the type.  

But the same above code works in the other compiler. Please run the code here

Program 2A

#include <iostream>

// A function to implement the sorting
template <std::size_t S>
void doSomething(int (&arr)[S])
{
    int i, j;
    int n = S;
    for (i = 0; i < n - 1; i++) {
	for (j = 0; j < n - i - 1; j++) {
	    if (arr[j] > arr[j + 1]) {
		std::swap(arr[j], arr[j + 1]);
	    }
	}
    }
    return;
}

// Function to print an array
void printArray(int *arr, int size)
{
     int i;
     for (i = 0; i < size; i++)
	std::cout << "i=" << i << "   " << arr[i] << "   ";
	std::cout << std::endl;
}


// Driver code
int main()
{
    int arr[] = { 5, 1, 4, 2, 8};
    int N = sizeof(arr) / sizeof(arr[0]);

    doSomething(arr);

    std::cout << "Sorted Original array: \n";
    printArray(arr, N);

    return 0;
}

Output:

1
2
Sorted Original array: 
i=0   1   i=1   2   i=2   4   i=3   5   i=4   8 

Template generally deduces the size from the variable passed. 

The argument 'arr' passed to the function 'doSomething'  is the reference to an array of compile time known size. 

Click here to debug the above program.

Program 2B

#include <iostream>

// A function to implement the sorting
template <typename T, std::size_t S>
void doSomething(T (&arr)[S])
{
    int i, j;
    int n = S;
    for (i = 0; i < n - 1; i++) {
	for (j = 0; j < n - i - 1; j++) {
	    if (arr[j] > arr[j + 1]) {
		std::swap(arr[j], arr[j + 1]);
	    }
	}
    }
    return;
}

// Function to print an array
void printArray(int arr[], int size)
{
     int i;
     for (i = 0; i < size; i++)
	std::cout << "i=" << i << "   " << arr[i] << "   ";
	std::cout << std::endl;
}


// Driver code
int main()
{
    int arr[] = { 5, 1, 4, 2, 8};
    int N = sizeof(arr) / sizeof(arr[0]);

    doSomething(arr);

    std::cout << "Sorted Original array: \n";
    printArray(arr, N);

    return 0;
}

Output:

1
2
Sorted Original array: 
i=0   1   i=1   2   i=2   4   i=3   5   i=4   8 

Program 2C using span 

#include <iostream>
#include <span>

// A function to implement the sorting
void doSomething(std::span<int> arr)
{
    int i, j;
    int n = arr.size();
    for (i = 0; i < n - 1; i++) {
 	    for (j = 0; j < n - i - 1; j++) {
	        if (arr[j] > arr[j + 1]) {
	         	std::swap(arr[j], arr[j + 1]);
	        }
	    }
    }
    return;
}

// Function to print an array
void printArray(int *arr, int size)
{
     int i;
     for (i = 0; i < size; i++)
	std::cout << "i=" << i << "   " << arr[i] << "   ";
	std::cout << std::endl;
}


// Driver code
int main()
{
    int arr[] = { 5, 1, 4, 2, 8};
    int N = sizeof(arr) / sizeof(arr[0]);

    doSomething(arr);

    std::cout << "Sorted Original array: \n";
    printArray(arr, N);

    return 0;
}

Output:

1
2
Sorted Original array: 
i=0   1   i=1   2   i=2   4   i=3   5   i=4   8 

Here is the code for the above program.

span is C++20 feature. 

Advantage: 

  • Reference to array can't be null. In program 2A/B, arr in doSomething, can't go null. But, if it is a raw pointer, it can be null. 
  • If it is a raw pointer, it can be incremented. References can't be incremented to point to the next address.  

Comments