c++ error: invalid types 'int[int]' for array subscript

May 2024 ยท 3 minute read

Trying to learn C++ and working through a simple exercise on arrays.

Basically, I've created a multidimensional array and I want to create a function that prints out the values.

The commented for-loop within Main() works fine, but when I try to turn that for-loop into a function, it doesn't work and for the life of me, I cannot see why.

#include <iostream> using namespace std; void printArray(int theArray[], int numberOfRows, int numberOfColumns); int main() { int sally[2][3] = {{2,3,4},{8,9,10}}; printArray(sally,2,3); // for(int rows = 0; rows < 2; rows++){ // for(int columns = 0; columns < 3; columns++){ // cout << sally[rows][columns] << " "; // } // cout << endl; // } } void printArray(int theArray[], int numberOfRows, int numberOfColumns){ for(int x = 0; x < numberOfRows; x++){ for(int y = 0; y < numberOfColumns; y++){ cout << theArray[x][y] << " "; } cout << endl; } } 
1

4 Answers

C++ inherits its syntax from C, and tries hard to maintain backward compatibility where the syntax matches. So passing arrays works just like C: the length information is lost.

However, C++ does provide a way to automatically pass the length information, using a reference (no backward compatibility concerns, C has no references):

template<int numberOfRows, int numberOfColumns> void printArray(int (&theArray)[numberOfRows][numberOfColumns]) { for(int x = 0; x < numberOfRows; x++){ for(int y = 0; y < numberOfColumns; y++){ cout << theArray[x][y] << " "; } cout << endl; } } 

Demonstration: http://ideone.com/MrYKz

Here's a variation that avoids the complicated array reference syntax: http://ideone.com/GVkxk

If the size is dynamic, you can't use either template version. You just need to know that C and C++ store array content in row-major order.

Code which works with variable size: http://ideone.com/kjHiR

4

Since theArray is multidimensional, you should specify the bounds of all its dimensions in the function prototype (except the first one):

void printArray(int theArray[][3], int numberOfRows, int numberOfColumns); 
3

I'm aware of the date of this post, but just for completeness and perhaps for future reference, the following is another solution. Although C++ offers many standard-library facilities (see std::vector or std::array) that makes programmer life easier in cases like this compared to the built-in array intrinsic low-level concepts, if you need anyway to call your printArray like so:

printArray(sally, 2, 3); 

you may redefine the function this way:

void printArray(int* theArray, int numberOfRows, int numberOfColumns){ for(int x = 0; x < numberOfRows; x++){ for(int y = 0; y < numberOfColumns; y++){ cout << theArray[x * numberOfColumns + y] << " "; } cout << endl; } } 

In particular, note the first argument and the subscript operation:

3

If you pass array as argument you must specify the size of dimensions except for the first dim. Compiler needs those to calculate the offset of each element in the array. Say you may let printArray like

void printArray(int theArray[][3], int numberOfRows, int numberOfColumns){ for(int x = 0; x < numberOfRows; x++){ for(int y = 0; y < numberOfColumns; y++){ cout << theArray[x][y] << " "; } cout << endl; } } 

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobWpuZWmBenvCZpyrqp%2Bneqq61ZqjopxdqcaxsdJmoKesmaPBbrLOq2SaqqKWxm6%2F1JuqnKqZpcE%3D