5

I have got a small problem with 1D array in c++. I have got a function line this:

void func(int (&array)[???])
{
    // some math here;

    "for" loop {
        array[i] = something;
    }
}

I call the functions somewhere in the code, and before I made math I'm not able to know dimension of the array. The array goes to the function as a reference!, because I need it in the main() function. How I can allocate array like this?, so array with ?? dimension goes to the function as reference then I have to put the dimension and write to it some values.

5
  • 5
    Really your question is a bit unclear. Is your problem allocation, access or how to pass an array to a function? Please show some actual code that illustrates your problem, or describe what you actually want to do. Commented Nov 2, 2010 at 10:16
  • An array of references is an unusual beast: you probably don't want to use references like that anyhow. Commented Nov 2, 2010 at 10:37
  • @Eamon: You can't have an array of references, the above code is a reference to an array. Commented Nov 2, 2010 at 10:53
  • @Eamon Nerbonne: I really think this is the best way to really give func an array and not a bare pointer that you will use like an array. Commented Nov 2, 2010 at 10:55
  • Ah right, I misread the declaration as the (invalid) int &*arr rather than the unusual but valid int *&arr. I've never given int&*arr any thought - interesting to note that it is fortunately not merely a bad idea, but invalid too :-). After all, you can to something fairly equivalent via the (equally unwise) struct intRef { int & ref; intRef(int& ref):ref(ref){}}; void func(intRef * array) {} - so there's nothing fundamentally problematic with a pointer to a reference, it's just... unnecessary. Commented Nov 2, 2010 at 12:04

6 Answers 6

14

Since you're using C++, why not use a std::vector<> instead?

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

4 Comments

Additional +1 for vector, I use them all the time anytime I dont how how big "something" is going to be.
Do you think that answer is any helpful to a beginner?
@Dave: yes it is, it helps him understand that using "array"s in c++ can be done better, especially in these simple situations.
I hope I'm not going to insult nykon if I say that concluding from his question I think he didn't grasp the concept of arrays and pointers yet. This makes him being able to read api documentation unlikely. It's even questionable if a C++ beginner is able to conclude the existence of the standard template library from the snippet "std::vector<>"
10

Other have mentioned that you should use std::vector in C++ and they are right.

But you can make your code work by making func a function template.

template <typename T, size_t N>
void func(T (&array)[N])
{
    // some math here;

    "for" loop {
        array[i] = something;
    }
}

4 Comments

The template function way is a prime example of: "how to implement functionality already present in the Standard Library"...
@rubenvb: What do you think is being "implemented" here?
I agree with these "you should use a vector"; OK; but this answer provide a way to solve and understand the main question of the OP...
@Roger, @Cedric: I wasn't criticizing the quality of the answer, it's quite valid, but it kind of circumvents/avoids the presence of std::vector::size() in my view of things.
8

Use a pointer, not a reference:

void func(int *a, int N);

Or, easier, use a vector:

void func(std::vector<int> &a);

Vectors can be allocated by simply saying

std::vector<int> a(10);

The number of elements can be retrieved using a.size().

Comments

5

If the array you pass to func is a stack array, and not a pointer, you can retain its size by using a function template:

template <class T, size_t N>
void func(T(&array)[N])
{
    size_t array_length = N; // or just use N directly
}

int main() 
{
    int array[4];
    func(array);
}

That said, as others have already pointed out, std::vector is probably the best solution here.

1 Comment

but unnecessary, because inside of func you got the template-parameter N which contains the same information (as a compile time constant).
0

As well as vector which has been suggested you could possibly use valarray which is also part of STL and is intended specificially to handle mathematical collections.

Comments

0

What you have to realize, is that arrays are pointers. A definition like int array[5] will allocate space for 5 integers on the stack and array will be the address of the first value. Thus, to access the first value in the array, you can write

array[0] or *array (which is the same as *(array + 0))

In the same way to retrieve the address of the third element, you can write

&array[2] or array + 2

Since arrays are pointers, you don't have to worry about the runtime size of your array if you would like to pass it to a function, simply pass it as a pointer:

void func(int *array)
{
    int size;
    //compute size of the array
    for (int i = 0; i < size; ++i)
    {
        //do whatever you want with array[i]
    }
}

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.