ARRAYS

Write your question here.

Can you pass an array element or the whole array to a function in C++?

If so, give an example for each one.


Are arrays passed by value or by reference? Why?
Arrays implicitly decayed to pointers when "passed by value" (although arrays are never actually passed by value or copied):

1
2
3
4
5
6
//All of these things are equivalent. Meaning they are all raw pointers. Can accept arrays of any size, despite what is in between the [ and ].
//However, this approach loses array flexibility and loses all knowledge of the array's size, since it is now a dumb pointer.
void foo(int my_array[]);
void foo(int my_array[234]);
void foo(int* my_array);


There is a way to pass stack-based arrays by reference. This is the syntax for doing so:
1
2
//Pass array by reference. Does not decay to a pointer. This means that only stack-based arrays of size 5 can be passed.
void foo(int (&my_array)[5]);


This can also be useful as a template, to obtain the size of an array implicitly without having to pass it in:
1
2
3
//Pass array by reference. Does not decay to a pointer. This means that only stack-based arrays of size N can be passed.
template<typename T, size_t N>
void foo(T (&my_array)[N]);
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

void fun(char a) { std::cout << a<<"\n"; }

void fun1(char a[]) { std::cout << a << "\n"; }

//neither above function would change foo in the main scope if there was a change

void fun2(char &a) {  //this changes foo element by element but kinda a crappy example

    a = 'A';
}

int main(){

    char foo[6] = "hello";

    fun(foo[1]); //should print e

    fun1(foo); //should print hello


    for (int i=0; i < sizeof(foo); i++) {
        fun2(foo[i]);
    }
  
    std::cout << foo << "\n";
}


but as was said everything in c++ is passed in by pointer.
Last edited on
but as was said everything in c++ is passed in by pointer.

This is not true. Arrays can be passed by reference and thus not decay to pointers, as I demonstrated. Also, the usage of sizeof the way you use in your example for anything larger than char arrays will result in a buffer overflow and subsequently a segfault. Also, this is wrong:

1
2
3
4
5
void fun(char a) { std::cout << a<<"\n"; }

void fun1(char a[]) { std::cout << a << "\n"; }

//neither above function would change foo in the main scope if there was a change 


This is not true. The second function WILL change the original array. As I said, arrays never copy elements by value. Trying to pass them by value will always make them decay to pointers. This is a rule of the language.

Please make sure you know that your advice is correct before attempting to advise others, or else you will end up confusing them even more.
Last edited on
nm I guess if you pass in an array it automatically passes it by reference. and apparently so does anytime you pass in a pointer. but regardless when you pass something to a function its still always a pointer or in the case of an array a list of pointers.

The difference is that when you pass in a value by ref its actually working with the existing pointer instead of just copying the value to a temp location.

Last edited on
foo doesn't change....

That is incorrect. First of all, the code you wrote sets the "a" pointer to a null pointer (sets it to zero), which is why it doesn't change the original array. Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <vector>

void fun1(char a[]) { a[11] = 's'; }


int main(){

char b[] = "Hello Worldz";
std::cout << std::string(b) << std::endl; // This will print "Hello Worldz"
fun1(b);
std::cout << std::string(b) << std::endl; // This will print "Hello Worlds". The original array has been changed.


}


See the output here:
http://coliru.stacked-crooked.com/a/6b58fed35a6c21cd

The original array is changed when you access the elements through the "a" pointer. All you are doing in your code is resetting the pointer to a nullptr, which does absolutely nothing to the array itself because you aren't actually changing the array, you are just changing the pointer that points to the array.

passing something by reference just implies that the original variable is changed in the scope it currently exists. Passing it it by value or via a pointer makes no difference bc theres and implicit conversion that takes place. Its still just a pointer being automatically dereferenced


You cannot pass an array by value. That is a language rule. lvalue array types passed to functions automatically decay to pointer types.
https://en.cppreference.com/w/cpp/language/array#Array-to-pointer_decay
Last edited on

regardless when you pass something to a function its still always a pointer or in the case of an array a list of pointers.


No, when you pass an array to a function, it is a single pointer that points to the first element of the array. It is not a list of pointers. In fact:

void foo(int ptr[]);

Is the exact same as

void foo (int* ptr);

There is no difference between them. You can pass pointers or arrays to either one and they will both be pointer types.
Last edited on
Topic archived. No new replies allowed.