-1
void print( matrix)
{
    printf("    0    1    2    3    4     5    6    7  \n"); /* printing the game board */
    printf("    -------------------------------------    \n");
    for (int i = 0; i < 8; i++)
    {
        printf("%d|", i);
        for (int j = 0; j < 8; j++)
        {
            printf(" %3c ", matrix[i][j]);
        }
        printf("\n");
    }
}

so I defined this function in the compiler, ran the code and get the following error message for this line printf(" %3c ",matrix[i][j]);

subscripted value is neither array nor pointer nor vector

I tried defining the matrix as int and as char value, but still same error

what I am trying to achieve by calling the function
enter image description here

one more thing is that the code itself without being a function works without a problem

calling the function: first I defined

char playboard[8][8];

then filled the array with the symbol "~"

for (int i = 0; i < 8; i++)
  {
     for (int j = 0; j < 8; j++)
       {
          playboard[i][j] = '~';
       }
  }

then called the function print

print(playboard);
Enigma
  • 129
  • 1
  • 7

2 Answers2

4

You probably want this:

void print(char matrix[8][8])
{
    printf("    0    1    2    3    4     5    6    7\n"); /* printing the game board */
    printf("    -------------------------------------\n");
    for (int i = 0; i < 8; i++)
    {
        printf("%d|", i);
        for (int j = 0; j < 8; j++)
        {
            printf(" %3c ", matrix[i][j]);
        }

        printf("\n");
    }
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
1

In addition to the Michael answer you should pass sizes of array to the function to make it portable. Take a look at this simple example using sizeof

#include <stdio.h>
#include <string.h>

void print(size_t rows, size_t cols, char matrix[rows][cols])
{
    printf("    0    1    2    3    4     5    6    7\n"); /* printing the game board */
    printf("    -------------------------------------\n");
    for (size_t i = 0; i < rows; i++)
    {
        printf("%zu|", i);
        for (size_t j = 0; j < cols; j++)
        {
            printf(" %3c ", matrix[i][j]);
        }

        printf("\n");
    }
}

int main (void)
{
    char playboard[8][8];

    memset(playboard, 0x7E, sizeof(playboard));

    print(sizeof(playboard[8]), sizeof(playboard[0])/sizeof(playboard[0][0]), playboard);
}
LPs
  • 16,045
  • 8
  • 30
  • 61
  • I am still a beginner when it comes to C, didn't totally get everything, but the first answer works perfectly thank you nonetheless – Enigma Apr 10 '17 at 15:28
  • I know it, and @MichaelWalz answer was perfect. Mine is only to add some details. – LPs Apr 10 '17 at 15:29