Problem in assembling an array in C

1

I'm having a problem with an array that I set to get typed letters and these are transformed into ascii table numbers, but it does not print Where am I going wrong? Can anyone help me?

#include <stdio.h>
#include <stdlib.h>
int main(){
 char i, j, m[3][3];
 char tecla[0];


 //captura os elementos
 for(i=0;i<3;i++)
 for(j=0;j<3;j++){
 printf("Elemento[%d][%d]= ",i,j);
 tecla[0] = getche();
 scanf("%c",&m[i][j]);
 }
 //EXIBIR VALORES ORIGINAIS
 printf("\n::: Valores Originais :::\n");
 for(i=0;i<3;i++){
 for(j=0;j<3;j++)
 printf("%c ",m[i][j]);
 tecla[0] = getche();
 printf("\n");
 }
    
asked by anonymous 27.10.2017 / 22:32

1 answer

3

I think what you wanted was this:

#include <stdio.h>
#include <stdlib.h>
int main() {
    char i, j, m[3][3];
    char tecla;

    //captura os elementos
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("Elemento[%d][%d] = ", i, j);
            tecla = getche();
            scanf("%c", &m[i][j]);
         }
     }

     //EXIBIR VALORES ORIGINAIS
     printf("\n::: Valores Originais :::\n");
     for (i = 0; i < 3; i++) {
         for (j = 0; j < 3; j++) {
             printf("%c ", m[i][j]);
         }
         printf("\n");
     }
     tecla = getche();
}

Always use { and } to avoid making mistakes. For example, in this code you posted:

 //EXIBIR VALORES ORIGINAIS
 printf("\n::: Valores Originais :::\n");
 for(i=0;i<3;i++){
 for(j=0;j<3;j++)
 printf("%c ",m[i][j]);
 tecla[0] = getche();
 printf("\n");
 }

Notice that the for inside does not have { . This causes tecla[0] = getche(); and printf("\n"); to be outside the for internal. If you always use { on all for ties, you will not have this problem. Idling everything properly will also help you a lot.

In addition, char tecla[0]; declares an array of zero positions and you write in the first position of it (position 0), which is already outside the array. This does not make sense, it is easiest to use only a common variable char tecla; .

    
27.10.2017 / 23:10