Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between reference to an array and array as a parameters in functions?

What is the difference between functions, which have reference to an array:

// reference to array
void f_(char (&t)[5]) {
    auto t2 = t;
}

and simply array:

// just array
void f__(char t[5]) {
    auto t2 = t;
}

as a parameters?

The calling code is:

char cArray[] = "TEST";
f_(cArray);
f__(cArray);

char (&rcArr)[5] = cArray;
f_(rcArr);
f__(rcArr);

In both cases t2 is char*, but in first function my VS2019 is showing that t inside function has type char(&t)[] and t inside second function has type char*.

So after all, is there any practical difference between those functions?

like image 605
hgrev Avatar asked Dec 17 '21 18:12

hgrev


People also ask

How do you reference an array in a function?

Arrays can be passed by reference OR by degrading to a pointer. For example, using char arr[1]; foo(char arr[]). , arr degrades to a pointer; while using char arr[1]; foo(char (&arr)[1]) , arr is passed as a reference. It's notable that the former form is often regarded as ill-formed since the dimension is lost.

Can a function have an array as a parameter?

In this tutorial, we will learn how to pass a single-dimensional and multidimensional array as a function parameter in C++ with the help of examples. In C++, we can pass arrays as an argument to a function. And, also we can return arrays from a function.

What is the difference between a function that passes parameters using call by reference and a function that uses call by value?

In the Call by Value method, there is no modification in the original value. In the Call by Reference method, there is a modification in the original value. In the case of Call by Value, when we pass the value of the parameter during the calling of the function, it copies them to the function's actual local argument.

Is int arr [] and int * arr the same?

No difference. At all. In a function parameter list, int arr[] is nothing but an "alternative" writing for int* arr .

What does it mean to reference an array?

Reference to an array means aliasing an array while retaining its identity. Reference to an array will not be an int* but an int []. Let us discuss this in detail by discussing the difference between these two.

What is the difference between an array and an object?

The object can be extended and the changes will be reflected in the whole prototype chain for other objects. Arrays are objects only in javascript. The major difference is that they store the data in an ordered collection in which the data can be accessed using a numerical index. They are also mutable and data can be modified at any index.

Is reference to an array an int* in C++?

Reference to an array will not be an int* but an int []. Let us discuss this in detail by discussing the difference between these two. This is quite weird that int [] is the same as int* but still compiler perspective on both is entirely different.

What is the difference between a and B in an array?

For compiler, a and b are the same data type i.e int* here for compiler they are just int* pointing to address. But for compiler type of as an array is int [2] and type of b as an array is int [3] which are completely different from each other.


Video Answer


1 Answers

You can specify a complete array type parameter as for example

void f( int ( &a )[N] );

and within the function you will know the number of elements in the passed array.

When the function is declared like

void f( int a[] );

then the compiler adjusts the function declaration like

void f( int *a );

and you are unable to determine the number of elements in the passed array. So you need to specify a second parameter like

void f( int *a, size_t n );

Also functions with a referenced array parameter type may be overloaded. For example these two declarations

void f( int ( &a )[] );

and

void f( int ( &a )[2] );

declare two different functions.

And functions with a referenced array parameter type may be called with a braced list (provided that the corresponding parameter has the qualifier const) like for example

f( { 1, 2, 3 } );

Here is a demonstration program

#include <iostream>

void f( const int ( &a )[] )
{
    std::cout << "void f( const int ( & )[] ) called.\n";
}

void f( const int ( &a )[2] )
{
    std::cout << "void f( const int ( & )[2] ) called.\n";
}

void f( const int a[] )
{
    std::cout << "void f( const int [] ) called.\n";
}


int main()
{
    f( { 1, 2, 3 } );
}

The program output is

void f( const int ( & )[] ) called.
like image 79
Vlad from Moscow Avatar answered Oct 18 '22 22:10

Vlad from Moscow