7

I wounder how i can make this code work?

#include <iostream>
using namespace std;

void writeTable(int (&tab)[],int x){
    for(int i=0;i<x;i++){
        cout << "Enter value " << i+1 <<endl;
        cin >> tab[i] ;
    }
}


int main(void){
    int howMany;
    cout << "How many elemets" << endl;
    cin >> howMany;

    int table[howMany];
    int (&ref)[howMany]=table;
    writeTable(ref,howMany);
    return 0;
}

And here are the errors that I have:

|4|error: parameter ‘tab’ includes reference to array of unknown bound ‘int []’|
|18|error: invalid initialization of reference of type ‘int (&)[]’ from expression of type ‘int [(((unsigned int)(((int)howMany) + -0x00000000000000001)) + 1)]’|
|4|error: in passing argument 1 of ‘void writeTable(int (&)[], int)’|

Thanks for help

0

5 Answers 5

18

If you are intending to pass the size of the array, then remove the reference

void f(int a[])

is equivalent to

void f(int* a)

so no copying will be done, if that is the concern.

If you want to take an array by reference, then you MUST specify the dimension. e.g.

void f(int (&a)[10])

Naturally, the best of the two is the third solution, which is to use std::vector's and pass them by reference, reference to const or by value if needed. HTH

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

5 Comments

Agree, passing in a pointer to the array is the way to go here.
Ok thx for help but i have one additional question in the third option I can pass dimension using a variable? If so how would look calling that function and it's prototype?
@Artur: See Prasson Saurav's answer and my comment to it. Or do you mean the vector's? For vector's see Amardeep's answer :)
@Artur - Why do you want to take the array by reference? What are you trying to achieve here versus the simpler way of passing in the pointer?
I must do this by using references I can't user any other technique such vectors etc now I know how to do this more less. Thx for help and explanations ;)
6

Here is a slightly more C++ style of doing it:

#include <iostream>
#include <vector>

void writeTable(std::vector<int> &tab)
{
    int val;

    for (unsigned int i=0; i<tab.size(); i++)
    {
        std::cout << "Enter value " << i+1 << std::endl;
        if (std::cin >> val)
        {
            tab[i] = val;
        }
    }
}


int main()
{
    int howMany;
    std::cout << "How many elements?" << std::endl;
    std::cin >> howMany;

    std::vector<int> table(howMany);
    writeTable(table);

    return 0;
}

6 Comments

+1, but a more standard/portable way is to declare i's type as vector<int>::size_type
@Armen: I usually use size_t but laziness set in. I wasn't aware of the vector<>::size_type. Thanks for the info.
@Amardeep: Well, USUALLY vector<T>::size_type is typedef'd as size_t, which in turn is USUALLY typedef'd as unsigned int. But there are no guarantees
@Amardeep - +1 - you could also show the vector<int>::iterator alternative vs tab[i]
@Steve: The only reason I kept the numeric loop counter was because the original functionality displayed the index number with each read. Otherwise a for_each or while would have been nicer. Thanks.
|
5

You need not specify the dimension of the array if you make writeTable a function template.

template <typename T,size_t N>
void writeTable(T (&tab)[N]) //Template argument deduction
{
    for(int i=0 ; i<N ; i++){
       // code ....
    }
}

.

int table[howMany]; // C++ doesn't have Variable Length Arrays. `howMany` must be a constant
writeTable(table);  // type and size of `table` is automatically deduced

3 Comments

but take heed that in this case you will not be able to pass dynamically allocated arrays to this function.
@Armen : Yes! Why would he need a dynamically allocated array anyway (he would use std::vector)?
My comment was intended for the OP, not you :)
1

Following Amardeep's answer, here is a C++11 way to do it:

#include <iostream>
#include <vector>

void writeTable(std::vector<int> &tab)
{
    int val;

    for (auto& cell : tab)
    {
        std::cout << "Enter value " << i+1 << std::endl;
        if (std::cin >> val)
        {
            cell = val;
        }
    }
}


int main()
{
    int howMany;
    std::cout << "How many elements?" << std::endl;
    std::cin >> howMany;

    std::vector<int> table(howMany);
    writeTable(table);

    return 0;
}

Note the range-based for used in writeTable.

Comments

0

Arrays of references are illegal, if that is what you are trying to do. It's not 100% clear to me from the title.

6 Comments

virtual -1: Obviously he is trying to create a reference to an array, not an array of references
@Armen - the syntax is confused enough that I respectfully disagree. It's likely that your answer is what he needs though.
@Steve: If you look at the code(let alone the syntax), it is VERY obvious that he was not trying to create an array of references
@Armen - the older I get, the less there is that is VERY obvious to me
@Steve: My youth is my enemy :)
|

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.