How to receive a character array in C with for?

Asked

Viewed 229 times

1

Considering Mxn as 2x2, I need to receive in each Mxn position a character.

for example:

r d
g h

for(i=0; i<m; i++){
        for(j=0; j<n; j++){
            printf("Digite um caractere para a posicao %d x %d da matriz: ",i,j);
            scanf("%c", &caractere[i][j]);
        }
    }

printf("%c",caractere[0][0]);
printf("%c",caractere[0][1]);
printf("%c",caractere[1][0]);
printf("%c",caractere[1][1]);

The point is:

If I put to receive inside the goes’s with scanf("%s", &caractere[i][j]); it looks like this: (I can type each letter in each position, but when it comes to saving, it takes the last two and overwrites the first two. - trying to type tgds, it saves dsds).

Digite um caractere para a posicao 0 x 0 da matriz: t 
Digite um caractere para a posicao 0 x 1 da matriz: g 
Digite um caractere para a posicao 1 x 0 da matriz: d 
Digite um caractere para a posicao 1 x 1 da matriz: s 
dsds

If I put to receive with scanf("%c", &caractere[i][j]); it looks like this: (I can’t type the two letters, it already puts the two printf’s together - as below.)

Digite um caractere para a posicao 0 x 0 da matriz: Digite um caractere para a posicao 0 x 1 da matriz: re 
Digite um caractere para a posicao 1 x 0 da matriz: Digite um caractere para a posicao 1 x 1 da matriz: e 
e

inserir a descrição da imagem aqui

How can I receive a character array in c? Would it really be two for? Why can’t I put in every position correctly?

*------------

put this way, he apparently picks one by one:

int main(int argc, char *argv[]) {
    int m = 0, n = 0, i, j;
    char caractere[m][n];
    
    m = 2;
    n = 2;
    
    for(i=0; i<m; i++){
        for(j=0; j<n; j++){
            printf("Digite um caractere para a posicao %d x %d da matriz: ",i,j);
            scanf(" %c", &caractere[i][j]);
        }
    }
 
    printf("\n%c",caractere[0][0]);
    printf("\t%c",caractere[0][1]);
    printf("\n%c",caractere[1][0]);
    printf("\t%c\n",caractere[1][1]);
    
    return 0;
}

however, look at the return: (continue, always taking the last 2 inserted.)

inserir a descrição da imagem aqui

  • 1

    I haven’t played with C in a while, but try to do this in your scanf scanf("%c", &(caractere[i][j])); is right to use two for is a for for each dimension of the array

  • 1

    as explanation the reference operator & is being applied only to caractere[i] and then he picks up the index j within the reference... so I suggested switching to include the parentheses like & applies to the caractere[i][j]

  • 1

    Are you typing a character into a ENTER? If so, the ENTER character is being read by the second scanf. Try to inform rdgh<ENTER> or clear the input buffer after each read,

  • @Jmslasher I tried to do this way scanf("%c", &(caractere[i][j]));, but still, he reads the scanf twice in a row, as the example I posted in the question (image).

  • @anonimo even without reading enter, in question when I use %s, it duplicates the last two entries. It would not be because of enter.

  • 1

    But if you are reading character then don’t use the format %s which is to read string. Use the %c. Another way to read the antrda with each character followed by a ENTER is to consume this ENTER with a space before %c: scanf(" %c", &caractere[i][j]);.

  • 1

    See test at: https://ideone.com/7VkVDP

  • @anonimo, face understood. I did exactly this way. But, look at the return. (I will post the question, to put the image)

  • Caraca, my stupid. @Anthonyaccioly worked. If you want to put as an answer, I accept. Okay.

Show 4 more comments

2 answers

2


Your code problem is in the following section:

int m = 0, n = 0, i, j;
char caractere[m][n];

This code is declaring a VLA (variable length array) empty once m and n are equal to 0.

If you change the values of m and n for 2 your code should work; that being said, if you know that your matrix is always 2X2 it is better to declare fixed sizes:

char caractere[2][2];

Vlas are part of the C99 standard, but in practice they are not widely used for a number of reasons beyond the scope of the question.

1

The way to fill the matrix is with 2 for’s even, this part is correct, it happens that the way your code is reading a character you typed and then the next scanf will read the "enter" you give when you finish typing. Putting the function getchar() Right after the scanf you guarantee that she will catch this "enter" that is giving you trouble. This would be the most correct way to do it, but it would also be possible to do it using the scanf("%s"), i tested on my machine your code with this case and it worked for any input, it can be a different version of compiler or libs giving different answers so really opt for the first alternative I presented.

  • Hey @Iagor, I put getchar(). however, it still prints only the last two, as I commented. I use Devc++ to compile. Is it some bug of his?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.