1

EDIT: found a solution probrably

I want to set 2d array a 2d return but it keeps giving me nonsense errors:

In function 'int main()':
error: expected primary-expression before ']' token
error: expected primary-expression before ']' token
In function 'int initBoard(int (*)[25])':
error: invalid conversion from 'int (*)[25]' to 'int' [-fpermissive]

I can't just figure out what is wrong and how to make error go away.

#include <iostream>

using namespace std;

const short WIDTH = 80;
const short HEIGHT = 25;

int clearBoard();
int initBoard(int board[WIDTH][HEIGHT]);
int drawBoard();

int main()
{
     int board[WIDTH][HEIGHT] = {{0}};
     board = initBoard(board); // problem is this place AND should be initBoard(board);
     cout << board[79][24]
     return 0;
}

int initBoard(int board[WIDTH][HEIGHT])
{
    unsigned int localWidth  = 1;
    unsigned int localHeight = 1;

    while(localHeight < HEIGHT)
    {
        while(localWidth < WIDTH)
        {
            board[localWidth][localHeight] = 0;
            localWidth++;
        }
        localHeight++;
        localWidth = 1;
    }
}
  • Welcome to Stack Overflow. Please read the [About] page soon. Are you sure the code you pasted generates the two errors about `]`? It shouldn't; it doesn't when I compile the code. (It complains about other things, legitimately, but it doesn't complain about that.) You might have to run the preprocessor on the program and look at the output, which could be daunting (I got 16437 lines of output from the first 17 lines of your code; fortunately, I only needed to look at the last 17 lines of output). If you're using `g++`, try `g++ -E program.cpp` and see whether the array definition is OK. – Jonathan Leffler Nov 20 '13 at 15:52

2 Answers2

1

Function initBoard has return type int:

int initBoard(int board[WIDTH][HEIGHT]);

You are trying to convert to type int type int ( * )[HEIGHT] inside the body of the function in the return statement and assign an object of type int to array in the statement that calls the function.

In C/C++ arrays have no assignment operator.

It will be enough to define the function the following way

void initBoard(int board[WIDTH][HEIGHT])
{
    unsigned int localWidth  = 1;
    unsigned int localHeight = 1;

    while(localHeight < HEIGHT)
    {
        while(localWidth < WIDTH)
        {
            board[localWidth][localHeight] = 0;
            localWidth++;
        }
        localHeight++;
        localWidth = 1;
    }
}

and to call it in main as

initBoard(board); 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-1

The array is always passed by reference, you don't need to explicitly return it. Any changes you make to it in the function are global. You're getting the compilation error because an 2d array is technically a "pointer to pointer to int", or int**. You can change initBoard to void and it should work. You don't assign arrays, you declare them.

You could do dynamic allocation instead of compile time, but since you seem to know what size the array should be, that's probably more trouble than it is worth.

Eldest
  • 1
  • 4
  • Further, the function is declared as returning `int` but you (the OP) are trying to return an array pointer as an `int`. In this context, the function should return `void` and the `return board;` statement should be deleted. – Jonathan Leffler Nov 20 '13 at 15:47
  • 1
    Oh dear; oh dear! No, an array is not an `int *`. I upvoted your original answer because it was accurate, if a little scant. I've now down-voted because it is wrong (but I'll remove my down-vote if you fix it). In many contexts, the name of an array converts to a `int *`. However, in this context, the array is a 2D array, and it becomes an `int (*)[25]`, a pointer to an array of 25 `int`. Also, you cannot assign arrays in C++ (or C). – Jonathan Leffler Nov 20 '13 at 15:54
  • You've revised it, but a 2D array is different from an `int **`. There are differences between arrays of a type and pointers a type. In some contexts, the two appear similar because an array is automatically converted to a pointer to the type, but there are still differences. The mechanics of looking up the value `matrix[i][j]` vary dramatically depending on whether you have `int matrix[10][20];` or `int **matrix;` (or, indeed, `int *matrix[10];`) as the definition of the variable. – Jonathan Leffler Nov 20 '13 at 17:21
  • Fair enough. I'll do some reading on the topic. – Eldest Nov 20 '13 at 18:10