3

I am in the process of learning c++. So i know a method by which you send something to function and then work as if it was call by value but actually it is call by reference. For example

void myFUNC(string& x)

Now, I have a dynamically created array of objects. I want to pass the array to function like a method above. Here's the code snippets

City *p = new City[number]; // pointer to dynamic array of class objects

//function prototype
void list_cities(int number, City p[]){
    for(int i=0; i<number; i++){
        cout<<p[i].get_name()<<endl;
    }
}
5
  • How to pass the dynamic array to function like the method above Commented Mar 4, 2015 at 17:31
  • list_cities(number, p); Commented Mar 4, 2015 at 17:36
  • 1
    BTW, prefer std::vector. Commented Mar 4, 2015 at 17:36
  • something easier? just help me out in sending the dynamic array to function Commented Mar 4, 2015 at 17:37
  • @FahadRana: What do you mean? There's nothing easier than list_cities(number, p) Commented Mar 4, 2015 at 17:40

3 Answers 3

3

Confusingly, a function parameter that looks like an array City p[] is actually a pointer, equivalent to City * p. So you can just pass a pointer to your dynamic array:

list_cities(number, p);

That said, prefer friendly types like std::vector<City> so you don't have to juggle pointers and carefully avoid memory leaks and worse.

Sign up to request clarification or add additional context in comments.

Comments

1

Arrays are by default converted to pointers, which are then passed as reference. So, there is no provision for explicitly passing arrays by reference. That is,

void list_cities(int number, City &p[])

is not p[] being passed as reference, but makes p[] an array of references.

Code:

void list_cities(int number, City p[])
{
    for(int i=0; i<number; i++)
    {
        cout<<p[i].get_name()<<endl;
    }
}

int main()
{
    City *p = new City[number];
    list_cities(number,p);
    delete[] p;
}

1 Comment

Followed a link here, arrays are not passed by reference, array to pointer conversion is performed. In this case, no array even exists, there is only a pointer.
1

Using std::vector you can also do the following:

void list_cities(std::vector<City> &cities)
{
    for (size_t iCity = 0; iCity < cities.size(); iCity++){
        cout << cities[iCity].get_name() << endl;
    }
}

std::vector<City> cities;
// fill list here
list_cities(cities);

2 Comments

I have no idea about vectors and they might solve but the other answers solved the problem.
You should definitely read about it. It's a container from the c++ Standard Template Library, which is basically a very thin wrapper around the dynamic array. But in contrast to dynamic arrays it provides basic functions e.g. for insertion or removing of elements and can handle the size of the array for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.