0

I am having difficulties passing dynamically allocated array to the function by reference. "The array should be transmitted to the function by reference". My program should take n amount of integers and find out the minimum. That minimum should be added to all the arrays that the user has entered. But the BY REFERENCE part kills me. I tried (int &x[],int &n) but failed. PLease help, thank you very much.

void add_min(int x[], int n) { int add[n]; int mini = x[0]; int i; for(i = 0; i < n; i++) { if(x[i] < mini) { mini = x[i]; } } for(i = 0; i < n; i++) { add[i] = x[i] + mini; } for(i = 0; i< n ; i++) { cout<<add[i]<<endl; } } int main() { int *x; int n; cout<<"Enter the amount of integers"<<endl; cin>>n; x = new int[n]; cout<<"Enter the integers"<<endl; for(unsigned i = 0; i < n; i++) { cin>>x[i]; } add_min(x,n); delete x; return 0; } 
4
  • there are two options: A) use std::vector instead of arrays B) this is homework and you are not allowed to use std::vector, in that case...well... you should use std::vector Commented Feb 3, 2016 at 10:18
  • If you are using a reference it has to be a reference to a known size array e.g. void add_min(int (&x)[10], int n). You should use a reference to a pointer void add_min(int *&x, int n). Commented Feb 3, 2016 at 10:27
  • Arrays are always passed as reference, right? So if you have a function that takes a pointer to an array, you can just pass in the array. It doesn't matter if it's &x[0] or x, since they mean the same thing. I'm not 100% sure however. Commented Feb 3, 2016 at 10:29
  • 1
    Also don't forget to use delete[] when you use array new! Commented Feb 3, 2016 at 10:33

3 Answers 3

2

Dynamically allocated arrays cannot be in a named variable and you cannot have a reference to one.

When you call new[] the array is allocated somewhere in the memory, and the call returns the address of the first object in that array. You store the address in a pointer variable. The pointer is not an array, it simply points to the first element.

You can pass a pointer by reference to a function like this:

void foo(int*& ptr) 

If your assignment asks you to pass an array by reference, then you may not allocate the array dynamically. In that case, this is how you declare the argument to be a reference to an array:

void foo(int (&arr)[10]) 

Note that the size must be known at compile time. You can use a template argument to generate functions for different sizes of arrays.

If the assignment does ask you to allocate the array dynamically, then they probably intend you to pass a pointer by reference.

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

5 Comments

"Dynamically allocated arrays cannot be in a variable" what do you mean?
@tobi303 I changed that to specify named variable, though it might be redundant. Wikipedia defines variable as a variable or scalar is a storage location paired with an associated symbolic name. Dynamically allocated objects have a storage location, but no symbolic name. In C++, Variables that have a symbolic name can have static or automatic storage, but as far as I know, you cannot have variables with dynamic storage.
Technically the wikipedia definition excludes references as variables, because they're not defined to have storage, but reference variables are defined by the standard explicitly. I couldn't find a general definition of a variable from the standard.
How do i call the function inside the main(): add_foo(arr); ??
@question (I removed the add_ prefix to be consistent with the other foo). Yes, in the case of the pointer reference version, you pass a pointer to the function, in the case of the array reference version, you pass a static or automatic array (of correct size) to the function.
0

The problem I see here is the following to pass in via reference a:

dynamically allocated array

The only way you can have dynamic allocation to happen is via returning a pointer. The syntax for passing in an array to a function, as you have correctly guessed is:

void add_min(int x[], int n) { ... } 

Certainly you can treat the array as a pointer, and then you get:

void add_min(int* x, int n) { ... } 

now comes the problem with the reference

The syntax for passing in an array via reference to the application is:

void add_min(int (&x)[3]) { ... } 

but in this case you need to actually know the size of it, so the array is not dynamically allocated.

To circumvent the need to know the size of the array, we can do some template magic, like:

template<int n> void add_min(int (&x)[n]) { ... } 

but again, this will need to be called with a statically allocated array (int x[124]), not a dynamic one.

But arrays are always passed in via reference, they are never copied to the stack, so your version of the code is correct.

2 Comments

"The syntax for passing in an array to a function, as you have correctly guessed is:" No, that is the syntax for passing a pointer into a function. This is already confusing enough. Better not confuse things further.
@juanchopanza Yup, you're right. I personally have the preference to write void f(int x[]) if I expect to pass in something that I know is an array (ie: more than one elements allocated in a pointer, or an actual array) and void f(int* x) if I know that what I will pass in is the address of a sole int, usually meant for returning a value. Sorry for the confusion.
0

First of all, you're using C++ so you shouldn't use pointers but std::vector. It'll be easier and you won't create memory leaks.

Second, you're passing pointer, not array. There are not the same: https://stackoverflow.com/a/1641963/1351828

So you're pssing the pointer to the first element. If you want to change elements in 'array' you can just pass pointer by value. If you want to allocate new 'array' and change it in a function, pass the pointer by reference:

void add_min(int &x[], int n) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.