1

I'm trying to program the game Minefield. I think the best way to make the field is to use a matrix of char. I ask the user for the difficulty, fill the matrix and then output the result just to see if that worked, but it doesn't, here's the code:

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

int selectDifficulty(){
    int d;
    do{
        printf("--------Selezionare difficolta' del campo minato--------\n");
        printf("1: Principiante (9x9)\n");
        printf("2: Intermedio (16x16)\n");
        printf("3: Esperto (30x16)\n");
        printf(">>>: ");
        scanf("%d", &d);
    }while(d<1 || d>3);
    return d;
}

int main(){
    int d,numCols,numRows;
    
    d = selectDifficulty();

    //Create numRows x numRows matrix based on difficulty d
    if(d==1){
        numCols=9;
        numRows=9;
    }
    else if (d==2){
        numCols=16;
        numRows=16;
    }
    else{
        numCols=30;
        numRows=16;   
    }
    
    char field[numRows][numCols];
    
    for(int i = 0; i < numRows; i++){      
        for(int j = 0; j < numCols; j++){
            field[i][j] = "a";
        }
    }
    for(int i = 0; i < numRows; i++){      
        for(int j = 0; j < numRows; j++){
            printf("%c",field[i][j]);
        }
        printf("\n");
    }
    return 0;
}

This is the output i get:

ÕÕÕÕÕÕÕÕÕ
ÕÕÕÕÕÕÕÕÕ
ÕÕÕÕÕÕÕÕÕ
ÕÕÕÕÕÕÕÕÕ
ÕÕÕÕÕÕÕÕÕ
ÕÕÕÕÕÕÕÕÕ
ÕÕÕÕÕÕÕÕÕ
ÕÕÕÕÕÕÕÕÕ
ÕÕÕÕÕÕÕÕÕ

What am i doing wrong?

I tried using malloc to make the matrix but i get the same output.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
VittorioDL
  • 39
  • 5

3 Answers3

1
field[i][j] = "a";

is assigning a pointer converted from the string literal (an array) "a" to field[i][j].

To store a character, you should do:

field[i][j] = 'a';

If you want to stick to use a string literal for some reason, you can do:

field[i][j] = "a"[0];

or:

field[i][j] = *"a";

Do turn on compiler warnings to catch simple errors like this.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
1

Ciao, as it was already pointed out you are using a string literal to initialize objects of the type char.

field[i][j] = "a";

In this case the string literal "a" is implicitly converted to a pointer to its first element of the type char * and in fact you have

field[i][j] = &"a"[0];

Instead of the string literal you have to use integer character constant 'a'

field[i][j] = 'a';

Also instead of the nested for loops

for(int i = 0; i < numRows; i++){      
    for(int j = 0; j < numCols; j++){
        field[i][j] = 'a';
    }
}

you could use standard C function memset declared in header <string.h>. For example

memset( field, 'a', sizeof( field ) );

Also you have a typo in the nested for loop that output the array

for(int i = 0; i < numRows; i++){      
    for(int j = 0; j < numRows; j++){
                   ^^^^^^^^^^^
        printf("%c",field[i][j]);
    }
    printf("\n");
}

Instead of numRows you have to use numCols in the inner for loop.

And to output the two-dimensional array you cold use only one for loop as for example

for(int i = 0; i < numRows; i++){ 
    printf( "%.*s\n", numCols, field[i] );       
}

Also these if statements

if(d==1){
    numCols=9;
    numRows=9;
}
else if (d==2){
    numCols=16;
    numRows=16;
}
else{
    numCols=30;
    numRows=16;   
}

would be more readable if to introduce an enumration.

enum { Principiante = 1, Intermedio = 2, Esperto = 3 };

switch ( d )
{
default:
case Principiante:
    numCols=9;
    numRows=9;
    break;

case Intermedio:
    numCols=16;
    numRows=16;
    break;

case Esperto:
    numCols=30;
    numRows=16;   
    break;
}

char field[numRows][numCols];

memset( field, 'a', sizeof( field ) );

for(int i = 0; i < numRows; i++){ 
    printf( "%.*s\n", numCols, field[i] );       
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

The key mistake is not enabling all warnings with a good compiler.

field[i][j] = "a";  // bad

... is readily warned as a problem.

warning: assignment to 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]

Fix with enabling many warnings.. Example: -pedantic -Wall -Wextra -Wconversion with gcc and:

field[i][j] = 'a';
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256