iffitheking iffitheking - 3 months ago 8
C Question

Multidimensional Array C

I am trying to print a board from user input (on command line) which suppose to print something like this

board

if I print without line number 23 i.e (

board[i][j] = i, j;
) my result is something like this.

board2

and if I add the above mentioned line, the compiler is giving me an error:


expression result unused [-Werror,-Wunused-value]


Please let me know what I am doing wrong.

#include <stdio.h>
#include <stdlib.h>

#define DIM_MAX 9

int d;
int board[DIM_MAX][DIM_MAX];

int main(int arg, char *argv[])
{
d = atoi(argv[1]);

for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
board[i][j] = i, j;
printf(" %i ", board[i][j]);
}
printf("\n");
}
}

Answer
board[i][j] = i, j;

is indeed your problem. To see why, look up the comma operator in C. https://en.wikipedia.org/wiki/Comma_operator

You do need to use both i and j on the right of the = on that line, but not as you are currently doing.

Comments