Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Pass a reference to an array to function

Given to functions void main() and void hello(byte* a[4]). Main function has an array of four bytes. The array's reference needs to be passed to the function hello for manipulation. I would expect the right syntax to be:

void hello(byte* a[4]){
 // Manipulate array
 a[0] = a[0]+1;
}

void main(){
 byte stuff[4] = {0,0,0,0};
 hello(&stuff);
 // hopefully stuff is now equal {1,0,0,0}
}

Alternatively I see others using this form of decaration:

void hello(byte (&a)[4])

Is this the right way to do it?

like image 312
Peter Savnik Avatar asked Jul 20 '17 17:07

Peter Savnik


People also ask

How do you reference an array in a function in C?

Passing arrays to functions in C/C++ are passed by reference. Even though we do not create a reference variable, the compiler passes the pointer to the array, making the original array available for the called function's use. Thus, if the function modifies the array, it will be reflected back to the original array.

How do you pass a copy of an array to a function in C?

Answer: An array can be passed to a function by value by declaring in the called function the array name with square brackets ( [ and ] ) attached to the end. When calling the function, simply pass the address of the array (that is, the array's name) to the called function.

How do you pass an array to a function explain?

In order to pass an array as call by value, we have to wrap the array inside a structure and have to assign the values to that array using an object of that structure. This will help us create a new copy of the array that we are passing as an argument to a function. Let's understand this with a simple example.

Does C pass arrays by value or reference?

Arrays are effectively passed by reference by default. Actually the value of the pointer to the first element is passed. Therefore the function or method receiving this can modify the values in the array.


2 Answers

There are many different options here depending on what you want to do here.

If you have a raw array of byte objects, you can pass it into a function like this:

void hello(byte arr[]) {
   // Do something with arr
}

int main() {
   byte arr[4];
   hello(arr);
}

The mechanism by which the array is passed into the function (a pointer to the first element of the array is passed to the function) functions similarly to pass-by-reference: any changes you make to arr in hello will stick in main even though you didn't explicitly pass in a reference to it. However, the hello function won't check whether the array has size four or not - it'll take in as input an array of any number of bytes.

You can also write

void hello(byte (&arr)[4]) {
   // ...
}

int main() {
    byte arr[4];
    hello(arr);
}

The syntax byte (&arr)[4] means "a reference to an array of four bytes." This explicitly passes the array by reference into hello, and it will check the size of the array to make sure it's correct. However, this is very unusual syntax and rarely seen in practice.

But perhaps the best idea is to not use raw arrays and to use std::array instead:

void hello(std::array<byte, 4>& arr) {
    // Do something with arr
}

int main() {
    std::array<byte, 4> arr;
    hello(arr);
}

Now, there's no weirdnesses about strange parentheses in the syntax for arrays of bytes and there's no worries about size checking. Everything is handled properly because std::array is an object type that has all the advantages of regular object types. I'd recommend going with this last approach above all the other ones.

like image 88
templatetypedef Avatar answered Sep 28 '22 14:09

templatetypedef


Arrays are already passed by pointer.

So this:

int a(int array[]) {
}

Is the same as doing this:

int a(int * array) {
}

Doing this:

void hello(byte (&a)[4])

only allows arrays with a length of 4 to be passed in.

like image 37
Cpp plus 1 Avatar answered Sep 28 '22 14:09

Cpp plus 1