Pass by refference pointer to array

Hi guys. I'm studying pointers right now and had already figured out how to pass pointer by reference into the function void (T *&name), then I've tried to pass pointer to array by reference into the function, but I've failed. I browsed a lot but didn't find a hint, only how to pass array by reference void (T (&name)[size]). So my qustion is: how to pass pointer to array by reference into the function and if it's possible?
There are only two reasons for passing something by reference.
1. Speed (but passing by value can be just as fast with some compiler optimizations)
2. To modify the variable locally, but also keep those modifications for wherever the function was called.

I can justify wanting to modify the array contents (which the pointer already allows you to do), but why would you want to modify the pointer to an array?
I wrote an example for passing a pointer to an array by reference anyways.
http://cpp.sh/8lwg3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iomanip>
#include <iostream>

void f(int* &xs) {
    xs = 0;
}

int main()
{
    int* array = new int[5];
    int* backup = array;
    std::cout << "Before f array: " << std::hex << array << std::endl;
    f(array);
    std::cout << "After f array: " << std::hex << array << std::endl;
    std::cout << "Luckily the backup is still: " << std::hex << backup << std::endl;
    std::cout << "Freeing memory using backup and exiting" << std::endl;
    delete[] backup;
    return 0;
}


Edit: I have also made an example that does not use the pointer syntax and instead uses the static syntax
http://cpp.sh/9p4d
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
#include <iostream>

void f(int xs[5]) {
    for(size_t i=0; i<5; ++i) {
        xs[i] += 5;
    }
}

int main()
{
    int xs[5];
    
    for(size_t i=0; i<5; ++i) {
        xs[i] = i+1;
        std::cout << xs[i] << ' ';
    }
    std::cout << std::endl << "Adding 5..." << std::endl;
    f(xs);
    for(size_t i=0; i<5; ++i) {
        std::cout << xs[i] << ' ';
    }
    std::cout << std::endl;
    
    return 0;
}

This shows that regular arrays are passed by reference. (This is because regular arrays are actually just constant pointers to memory that is guaranteed to be allocated.)
Last edited on
Thanks! So as I understand there is the only way to pass pointer to array by value void (T (*name)[size]), but no way to pass that pointer to array by reference, only the way with usual pointer (your first sample).
Last edited on
So as I understand there is the only way to pass pointer to array by value
Pointers are passed by value such as xs in void f(int xs[5])
or such as xs in void f(int* xs)

It makes no sense to pass a pointer to a statically allocated array by reference, and in my example you can see that it also does not make much sense to pass a pointer to a dynamic array by reference either.
Thanks, looks much more clear for me.

Looking at your samples, I see that the best ways to work with arrays or dynamic arrays is through the pointers. Like this:
T* ptr = new T[size]; T arr[size] = { ... }; T* ptr = &arr[0];
So if we can to work with pointers like in samples above, what is the purpose of such pointer T arr[size] = { ... }; T (*ptr)[size] = &arr; . In a book I found explanation of such syntax but without any practical purpose, what the difference between it and samples above? Thanks.
Last edited on
what is the purpose of such pointer
without any practical purpose

There is no purpose for aliasing arr to ptr. In fact, aliasing is generally a bad thing to do as it has the possibility of introducing bugs and ruining optimization of the compiler
Topic archived. No new replies allowed.