Home > Software engineering >  C Modify an array passed by parametres
C Modify an array passed by parametres

Time:10-18

I'm new at c and I'm having some difficulty on passing an array to a function.

I want to create an array of type Tableau (it initialize automatically at 4) and pass that array to another function, so passing distanceReste to calculReste.

I want to be able to change the array inside the other function, so for example, I want to add 1,2 and 3 and make the changes take place even after I leave that function.

So in my code, I would create the array, send it to calculReste, put the result of calculReste is reste and keep the changes to my array distanceReste I made so then I can use it in the function calculDepartReste (I didn't put it in the code).

Here's my code:

 Tableau<double> distanceReste[3];
    Tableau<double> reste = calculReste(distanceReste);
    Tableau<unsigned int> result = calculDepartReste(depart,distanceReste,reste);


    return result;

Tableau<double> Carte::calculReste (Tableau<double> *distanceReste[]) const {
    double max = std::numeric_limits<double>::max();
    Tableau<double> chemin;
    int position = 0;
    for(int i=0;i<3;i  ) {
        for (int j = 0; j < 3; j  ) {
            for (int k = 0; k < 3; k  ) {
                ...
                }
            }
        }
        *distanceReste[i] = max;
        max = std::numeric_limits<double>::max();

        position  = 3;
    }



    return chemin;
}

I kept it lighter to read it better.

I'd really appreciate it if you could help me out.

Thank you very much

Edit***

After seeing the answers that you guys gave me, i tried the following and it's still giving me an error: enter image description here

Here's the function: enter image description here

And he're the declaration of my functions in the header:

enter image description here

Still not working, I don't understand why?

CodePudding user response:

you don't have to pass the array with pointers because array in it default takes the address of the first element.so try the formal parameter without *.

CodePudding user response:

You can use function templates to pass the array by reference as shown below:

#include <iostream>

template<std::size_t N>
void func(int (&arr)[N])
{
    std::cout<<"size of array is: "<<N<<std::endl;
    std::cout<<"its elements are: "<<std::endl;
    for(int elem: arr)
    {
        std::cout<<elem<<std::endl;
    }
}


int main()
{
   
    int arrayToBePassed[4] = {1,45,23,54};
    func(arrayToBePassed);
    return 0;
}

So you have 2 options to solve your problem:

Solution 1

Make your calculReste function a member function template .

Solution 2

Pass the array be reference directly instead of pointer as shown below:

//note distanceReste is a reference to any array of fixed size 
Tableau<double> Carte::calculReste (Tableau<double> (&distanceReste)[4]) const {
    double max = std::numeric_limits<double>::max();
    Tableau<double> chemin;
    int position = 0;
    for(int i=0;i<3;i  ) {
        for (int j = 0; j < 3; j  ) {
            for (int k = 0; k < 3; k  ) {
                ...
                }
            }
        }
        *distanceReste[i] = max;
        max = std::numeric_limits<double>::max();

        position  = 3;
    }



    return chemin;
}

Also the declaration of calculReste inside the class will be changed to(if you choose solution 2):

Tableau<double> calculReste (Tableau<double> (&distanceReste)[4]) const;
  • Related