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

Asked

Viewed 297 times

0

I hosted the code complete in the Pastebin: https://pastebin.com/feKaxAiz. It is a matrix where it is possible to perform the SUMMING UP or AVERAGE of the elements above the MAIN DIAGONAL. It is rather extensive, but the first problem is in the MAIN, in the scanf of "OP", which references the operation the user wants to perform. I read with %c and he just ignore the scanf, ending the program, however, if I use %s, it allows OP reading and 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 p/ this. Besides, I’d like to know how the INCREMENT on a pointer. In function somaUpDP (summing the elements above the main diagonal), I use the pointer *cont to store both elements above D.P and thus be able to average.

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 count using the operator ++ increment in shape *cont++, but it didn’t work.

1 answer

1

Let’s see...

They’re just conceptual problems. The first of them is more "boring": understanding the reason that led to the problem itself requires a good understanding of how the scanf. The second, just more attention that you can see calmly!


1 - SCANF

The problem related to scanf is common. That is due to the ENTER that you press at some point prior (function leMatriz(...)): it translates into the character \n. As variables char read only one character, the line jump is in the buffer. At next reading, it is automatically captured in the variable used by scanf.

A simple way around this is to always use:

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

It is not the most elegant solution and will not work for all situations. The best way is by using the function fgets instead of the function scanf. @pmg wrote a 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 - (sum and subtraction).

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

*meuPonteiro++;

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

Therefore, "you" is not exactly reading what you think you are reading since you ended up changing the address stored by the pointer. The right way to do it is:

(*meuPonteiro)++;

So you increase the content in the address stored by the pointer. Parentheses are essential to prioritize.

Browser other questions tagged

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