Search code examples
cfor-loopmultidimensional-arraycharvariable-assignment

I get different output when printing a matrix of char


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.


Solution

  • 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] );       
    }