-1
void byReference(int (&p)[3]){
    int q[3] = {8, 9, 10};
    p = q;
}

I want to write function where i can reassign the p with new array. I am not sure if we can do that.

My goal : i want to change the original array, like we do swapping of two number by call-by reference.

Edit:

my working solution :

void byReference(int*& p){
    int* q = new int[2];
    q[0] = 8;
    q[1] = 9;
    
    p = q;
}

int main(){
    int *x = new int[2];
    x[0] = 1;
    x[1] = 2;

    byReference(x);
    
    return 0;
}
  • Local variable/array definitions won't outlive your function call. You need dynamic memory allocation, or pass properly beforehand allocated memory to your function. Your actual code more or less boils down to [this](https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope). – πάντα ῥεῖ May 07 '22 at 15:19
  • 2
    Better use `std::vector` or `std::array` (if the size is fixed). Then you can pass it by reference and modify in the function. – wohlstad May 07 '22 at 15:20
  • i want to change the original array, like we do swapping of two number by call-by reference. – niraj thakur May 07 '22 at 15:23
  • Your working solution is wrong. Your program is LEAKING memory. – digito_evo May 07 '22 at 16:06
  • @digito_evo I know that. That can fixed easily, i just wanted to test the reference variable concept. – niraj thakur May 07 '22 at 18:22

3 Answers3

0

In c++ it is recomended to use std::array for fixed size arrays, and std::vector for a dynamic size arrays.

Both of them can be passed by refernce, to be modified by a function. This requires the function to declare that the argument is passed by refernce using the & symbol.

See the example below:

#include <array>
#include <vector>

// Get an array (with a fixed size of 3) by refernce and modify it:
void ModifyStdArray(std::array<int, 3> & a) {
    a = std::array<int, 3>{8, 9, 10};
    // or:
    a[0] = 8;
    a[1] = 9;
    // etc.
}

// Get a vector by refernce and modify it:
void ModifyStdVector(std::vector<int> & v) {
    v = std::vector<int>{ 1,2,3,4 };
    // or:
    v.clear();
    v.push_back(1);
    v.push_back(2);
    // etc.
}

int main()
{
    std::array<int, 3> a1;
    // Pass a1 by reference:
    ModifyStdArray(a1);
    // Here a1 will be modified.

    std::vector<int> v1;
    // Pass v1 by reference:
    ModifyStdVector(v1);
    // Here v1 will be modified.
}
wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • can't we do the same with native array, without any dependency ? – niraj thakur May 07 '22 at 15:36
  • 2
    It can be done but very much not recomended. Why don't you want to use the standard `std::array` (or `std::vector`) ? – wohlstad May 07 '22 at 15:39
  • I am preparing for video where i want to show that reference variable can be used to change the original variable. I have example for normal int variable, but i am not getting how we can do the same for array variable. – niraj thakur May 07 '22 at 15:41
  • @nirajthakur You could use a native array and you wouldn't need std::array or std::vector. Furthermore, you could write your program in assembly and you wouldn't need C++. You could even enter the byte code manually and you wouldn't even need a compiler of any kind. (I hope the irony is not lost.) – Wyck May 07 '22 at 15:42
  • @nirajthakur If you allocate the native array before calling, you can use the solution pm100 suggested. However - if you are using c++ I really don't see a reason to do that. – wohlstad May 07 '22 at 15:43
  • @wohlstad I got why your solution is working.. i will dig more into it . Thank you. – niraj thakur May 07 '22 at 15:45
  • @Wyck i do get what you want to say. I am new to c++. Hope you forgive me for my silly ques. – niraj thakur May 07 '22 at 15:47
0

You can do it like this:

#include <iostream>
#include <array>
#include <algorithm>

// with std::array
void byReference( std::array<int, 3>& p )
{
    const std::array<int, 3> q { 8, 9, 10 };
    std::copy_n( std::begin( q ), std::size( p ), std::begin( p ) ); // copy q to p
}

// with C-style arrays
void byReference( int (&p)[ 3 ] )
{
    const int q[ 3 ] { 8, 9, 10 };
    std::copy_n( std::begin( q ), std::size( p ), std::begin( p ) ); // copy q to p
}


int main( )
{
    std::array<int, 3> arr;
    // int arr[ 3 ]; // or this one

    byReference( arr );

    std::cout << "The array is: ";
    std::copy_n( std::begin( arr ), std::size( arr ),
                 std::ostream_iterator<int>( std::cout, " " ) ); // copy the elements of arr to
                                                                 // the stdout (aka print them)
    std::cout << '\n';
}

Output:

The array is: 8 9 10 
digito_evo
  • 3,216
  • 2
  • 14
  • 42
0

You cannot copy arrays by assignment. You can use std::copy

void byReference(int(&p)[3]) {
    int q[3] = { 8, 9, 10 };
  //  p = q;
    std::copy(&q[0], &q[3], &p[0]);
}

....

int a[3] = { 1, 2, 3 };
byReference(a);

// a now 8,9,10
pm100
  • 48,078
  • 23
  • 82
  • 145