Function with a pointer?

Asked

Viewed 106 times

4

Enunciation:

A student’s grade is calculated from three grades awarded from 0 to 10 respectively to laboratory, a bimonthly evaluation and a final examination. The average three notes mentioned above follows the steps: work of laboratory: 2; semi-annual evaluation: 3; final examination: 5. According to the result, show on the screen if the student is flunked (average between 0 and 2,9), recovery (between 3 and 4,9) or if it was approved. Knife all necessary checks.

I had done it with a float function, which returned the variable. For reasons of learning I decided to try to do with pointer, but it didn’t work very well.

In the part of the function that will validate the note

while (var < 0 || var > 10)

If I type 5 for example, he gives 0 and 1, but I don’t quite understand why then 0 or 1 of 1, then he enters and stays in a loop asking to type a valid note. Why does this happen?

Follows the code:

#include <stdio.h>
#include <locale.h>

void validar(float *var){
    scanf("%f", var);

    while (var < 0 || var > 10){
        printf("\nNota invalida, tente novamente: ");
        scanf("%f", var);
    }
}

int main(){
    setlocale(LC_ALL, "portuguese");

    float lab, avaliacao, exame;

    printf("Entre com a nota do trabalho de laboratorio: ");
    validar(&lab);

    printf("Entre com a nota da avaliação bimestral: ");
    validar(&avaliacao);

    printf("Entre com a nota do exame final: ");
    validar(&exame);

    float media = (lab * 2 + avaliacao * 3 + exame * 5) / (2+3+5);

    printf((media < 3) ? "\nAluno reprovado.\n" : (media < 5) ? "\nAluno em recuperação\n" : "\nAluno aprovado\n");
}

2 answers

6

When you are going to learn a new concept first study a lot what it is before and start doing, so you can understand what is happening and enjoy much more. Just making it work is not exactly what I would call learning.

You passed the variable as a pointer. You understand that you are passing a pointer to a memory address where the value is actually and not the value?

Therefore in the scanf() do not need to pass with the address operator & (as exemplified in Why you don’t need the `&`scanf();`?), where it is an address that he expects, the variable var is already an address.

In other places you don’t expect an address, you expect a value, so you can use the variable directly, you have to do an operation that takes the address value. This is called dereference. The operator shall be used * to tell the compiler that you want the value that is in the address such. There it works. Note that despite being the same character, in this context it is neither the multiplication, nor the type indicator being a pointer.

while (*var < 0 || *var > 10) {
    printf("\nNota invalida, tente novamente: ");
    scanf("%f", var);
}

So it gets better, has less redundant code:

#include <stdio.h>

void validar(float *var){
    while (1) {
        scanf("%f", var);
        if (*var >= 0 && *var <= 10) return;
        printf("\nNota invalida, tente novamente: ");
    }
}

int main() {
    float lab, avaliacao, exame;
    printf("Entre com a nota do trabalho de laboratorio: ");
    validar(&lab);
    printf("Entre com a nota da avaliação bimestral: ");
    validar(&avaliacao);
    printf("Entre com a nota do exame final: ");
    validar(&exame);
    float media = (lab * 2 + avaliacao * 3 + exame * 5) / (2 + 3 + 5);
    printf((media < 3) ? "\nAluno reprovado.\n" : (media < 5) ? "\nAluno em recuperação\n" : "\nAluno aprovado\n");
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

But congratulations for having improved on what you already knew, many questions here we see the person continue to err on the same things.

4


while (var < 0 || var > 10)

In this case you want to validate (*var), when placing var is using the address

The correct answer would be while ((*var) < 0 || (*var) > 10)

As we make:

 printf("%d\n", var);
 printf("%f\n", *var);

OUTPUT with *var=10:

6356744
10,000000

Browser other questions tagged

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