Reading CHAR in C that works with %s but not with % c, why? And how does increment on a pointer work?

I hosted The Complete code in PASTEBIN: https://pastebin.com/feKaxAiz . this is a matrix where it is possible to perform the sum or mean of the elements above the main DIAGONAL. It is somewhat extensive, but the first problem is in MAIN, in the scanf of "OP", which refers to the operation that the user wants to perform. I did the read with %C and it simply ignores the scanf , terminating the program, however, if I use %s, it allows reading OP and the program works normally.

int main(){
int l, c;
int matriz[MAXL][MAXC];

numLC(&l, &c);
leMatriz(matriz, l, c);
printMatriz(matriz, l, c);

int soma = 0, media, cont = 0;
char OP;

printf("\nescolha uma operacao p/ ser feita com os elementos acima da DIAGONAL PRINCIPAL.\nS = soma | M = media: ");
scanf("%c", &OP);

somaUpDP(matriz, l, c, &soma, &cont);
//printf("\nsoma teste = %d", soma);
//printf("\nnum elementos teste = %d", cont);
operacao(OP, soma, cont);
return 0;

I would like an explanation for this. What's more, I would like to know how increment on a pointer works. In the function somaUpDP (which performs the sum of the elements above the main diagonal), I use the pointer * cont to store the amount of elements above the D. P and thus be able to perform the media.

void somaUpDP(int matriz[][MAXC], int l, int c, int *soma, int *cont){ //realiza a soma dos elementos ACIMA da DIAGONAL PRINCIPAL.
for(int i = 0; i < l; i++){
    for(int j = 0; j < c; j++){
        if(j > i){
            *soma+=matriz[i][j]; //soma os elementos acima da D.P.
            *cont = *cont + 1; //pega o tanto de elementos acima da D.P (necessário p/ média).
            //*cont++ não funciona (?).
        }
    }
}

I tried to do the count using the operator ++ of increment in the form * cont {, but it did not work.

Author: qvvert, 2019-10-21

1 answers

Let's see...

Are conceptual problems only. The first of them is more "chatinho": understanding the reason that led to the existence of the problem itself requires understanding well how scanf works. Already the second, it is enough more attention that you can see calmly!


1 - SCANF

The problem related to scanf is common. This is due to ENTER that you press at some previous time (function leMatriz(...)): it translate no character \n. Since variables char read only one character, the line break is in the buffer. On the next read, it is automatically captured in the variable used by scanf.

A simple way around this is to always use:

scanf("%c\n", &meuChar);

Is not the most elegant solution and will not work for all situations. The best way is to use the function fgets instead of the function scanf. @Pmg wrote an elegant response with a beautiful example. I recommend reading.


2 - INCREMENT OF POINTERS

There are priorities between operators . For example: * and / (multiplication and division) have priority over + and - (addition and subtraction).

In C, ++ has priority over * (pointer deference). So if you do:

*meuPonteiro++;

What actually occurs is an increment of the value within the pointer. This value is the address of memory. Thus, you increment one at this address. Then deference occurs.

So "you" are not exactly reading what you think you are reading since you ended up changing the address stored by the pointer. The correct way to do this is:

(*meuPonteiro)++;

So you increment the content in the address stored by the pointer. Parentheses are essential for setting priorities.

 0
Author: José, 2019-10-21 08:24:31