0

I have two for-loops that process data from a file and store it into an array, conveniently indexing each element appropriately. I want to pass this array into a function and perform some calculations on it but it gives me an error: "error: no matching function for call to 'year_range'".

I have tried to pass it as item[][] but then the compiler wants me to specify the index. I want to pass the entire array into the function so I can perform some calculations on it.

void year_range(string item[]);

int main()
{
    string item[75][5];
    for (int row = 0; row < 74; row++)
    {
        for (int col = 0; col < 4; col++)
        {
            getline(fin,item[row][col], ',');
        }
        getline(fin, item[row][4], '\n');  
    }
    year_range(item);
}

void year_range(string item[])
{
  // processing item array
}

I expect that the entire "item" array successfully passes through the function year_range, with no errors.

  • 2
    Tip: Don't. Use `std::vector` in C++. Using C arrays leads to a whole world of hurt. You'll also want to get in the habit of passing in things by reference instead of as arrays/pointers, plus using Standard Library iterators and things like the [`for (auto x : y)`](https://en.cppreference.com/w/cpp/language/range-for) notation. – tadman Oct 18 '19 at 00:48

2 Answers2

3

It wants the length of the second dimension. Try:

void year_range(string item[][5]);

Note that this only works for arrays that have a second dimension of 5. For better, more general ways to do this, see this semi-duplicate question.

Also note: You will have to change this in both your implementation and declaration, even though I don't show changing it in your implementation.

  • Note: I would call this a semi-duplicate because the question asks about `**` notation and not `[][num]` notation. But if others feel it is a dupe, feel free to close. –  Oct 18 '19 at 00:57
  • It gives me this error: "error: expected expression year_range(item[][5]);" – Gorav Soni Oct 18 '19 at 00:58
  • Did you change it both places? In both the declaration and implementation? –  Oct 18 '19 at 00:59
  • omg yes it's working, that's a silly mistake. Thanks so much, been stuck on this for a while :) – Gorav Soni Oct 18 '19 at 01:02
  • You're welcome. If this solved your problem, feel free to accept this answer by clicking the check mark next to the answer. –  Oct 18 '19 at 01:04
1
void year_range(string item[]);

Here, the argument is of type "pointer to string".

string item[75][5];
year_range(item);

When you use an array as a value, it will implicitly convert to a pointer to first element. This is called decaying. item is an array of (75) arrays of 5 strings. Since the elements are arrays of 5 strings, a pointer to an element is a pointer to an array of 5 strings.

So you are passing a pointer to an array of 5 strings into a function that accepts a pointer to a string. An array of 5 strings is a different type from a strings, so this cannot work. You can change year_range to accept a pointer to 5 strings, which matches the (decayed) object that you're attempting to pass:

void year_range(string item[][5]);  // like this
void year_range(string (*item)[5]); // or this
eerorika
  • 181,943
  • 10
  • 144
  • 256