Passage by reference in C

Asked

Viewed 68 times

1

My IDE is giving error in line:

float CalculaHora(horas,minutos,segundos,&conversao);

It indicates that you need a parenthesis before the operator &:

#include <stdio.h>

int main() {
 float horas, minutos, segundos, conversao;
 printf("Digite a hora, minutos e segundos: ");
 scanf("%f %f %f", &horas,&minutos,&segundos);
 float CalculaHora(horas,minutos,segundos,&conversao);
 printf("A quantidade de segundos eh %f !\n", conversao);
 system("pause");
 return 0;
}
float CalculaHora(float horas,float minutos,float segundos,float *pConversao){
if (horas > 0){
    horas = horas /3600;
         if (minutos > 0)
         minutos = minutos / 60;
            if (segundos > 0)
            *pConversao = minutos + horas + segundos;}
else {
    if (minutos > 0){
    minutos = minutos / 60;
        if (segundos > 0)
        *pConversao = minutos + segundos;}
            else{
                if (segundos > 0)
                *pConversao = segundos;}
        }

return *pConversao;
 }

People wanted to say I was based on that code, so I didn’t understand:

void CalculaConceito(float media, char *pConceito){
 if (media >= 6) 
 *pConceito = 'A';
 else 
 *pConceito = 'R'; }
int main() {
 float media;
 char conceito;
 printf("Digite a media: ");
 scanf(" %f", &media);
 CalculaConceito(media, &conceito);
 printf("O conceito e %c !\n", conceito);
 system("pause");
 return 0;
}
  • Again I will draw your attention to: a function must be defined either before main or after main, provided that its signature (or protop) is declared before main. Do not sign within of main.

3 answers

2


First, that line inside the main:

float CalculaHora(horas, minutos, segundos, &conversao);

This is an attempt to function statement, shouldn’t be there. On this line you should be calling for the function.

An outline of what it would be (in the end we will see the complete code):

// função CalculaHora (declaração da função)
float CalculaHora(float horas, float minutos, float segundos, float *pConversao) {
    // código que calcula...
}

int main() {
    float horas, minutos, segundos, conversao;
    // printf, scanf...

    // CHAMAR a função
    CalculaHora(horas, minutos, segundos, &conversao);
}

Notice that to call the function I don’t need to put float in front. The float CalculaHora is used in function declaration to say "this is function CalculaHora, that returns a float". When it comes to calling, you just... call, without putting the guy in front.


Now the question of * versus &.

In the function statement you use float *pConversao, for pConversao is a pointer to float (the asterisk indicates this, which is a pointer). This means that the function waits for a pointer there: when you call the function, you have to pass a pointer.

Now on the main, the variable conversao is not a pointer: she is a float. But to call the function CalculaHora, you need to pass a pointer to float. And how do you pass a pointer to the variable float? Putting the & before, indicating that you are passing her address (i.e., a pointer to float, which is precisely what the function CalculaHora waiting).


That being said, it makes no sense to change the function at the same time the value of the pointer and return it. If it already returns, it would not need to receive the pointer.

And from what I understand, the calculation is wrong. If you get, for example, 1 hour, 10 minutes and 30 seconds, the amount of seconds would not be 4230?

Anyway, returning: if the function receives a pointer and modifies its value within it, there is no reason to return that same value, then it could be like this:

// void, pois não precisa retornar *pConversao (já que o valor é modificado, então o return é redundante)
void CalculaHora(float horas, float minutos, float segundos, float *pConversao) {
    *pConversao = horas * 3600 + minutos * 60 + segundos;
}

int main() {
    float horas, minutos, segundos, conversao;
    printf("Digite a hora, minutos e segundos: ");
    scanf("%f %f %f", &horas, &minutos, &segundos);
    CalculaHora(horas, minutos, segundos, &conversao);
    printf("A quantidade de segundos eh %f!\n", conversao);
    return 0;
}

But if you want the function to return the value, then you wouldn’t actually need a pointer:

// retorna o valor, então não precisa receber o ponteiro
float CalculaHora(float horas, float minutos, float segundos) {
    return horas * 3600 + minutos * 60 + segundos;
}

int main() {
    float horas, minutos, segundos, conversao;
    printf("Digite a hora, minutos e segundos: ");
    scanf("%f %f %f", &horas, &minutos, &segundos);

    // variável recebe o valor retornado pela função
    conversao = CalculaHora(horas, minutos, segundos);
    printf("A quantidade de segundos eh %f!\n", conversao);
    return 0;
}
  • simplified the code, thanks man, I tested the function below the main but did not work, Oce knows why?

  • @Cl2727 Because in C vc you need to declare the function before using it: https://answall.com/q/366060/112052

1

Entering only the merit of the error pointed out in the question, on the line where "flame" its function main:

float CalculaHora(horas,minutos,segundos,&conversao);

Note that you are not actually calling the function (see float at the beginning of the call). You are doing something close to prototype declaration of a function.

So you can call your function correctly, declare your prototype with the types of parameters at the beginning of your file and call the function later. The result will look like the code block below (note that there is no longer a "float" in the call):

float CalculaHora(float horas, float minutos, float segundos, float *conversao);

int main() {
    float horas, minutos, segundos;
     
    float* conversao = malloc(sizeof(float));
     
    printf("Digite a hora, minutos e segundos: ");
     
    scanf("%f %f %f", &horas,&minutos,&segundos);
     
    CalculaHora(horas, minutos, segundos, conversao);

...

In the second example of question code, note that the argument being passed to the function is not a pointer, and yes a char. So your memory address is passed using the operator & which returns a pointer:

CalculaConceito(..., &conceito);

With the function receiving a pointer in its signature:

void CalculaConceito(float media, char *pConceito)

Thus the value to which it points can be accessed with the operator *:

*pConceito = 'R';

Note that the function statement does not return any value, having void in your signature. This means that the variable conceito in function main is being amended by reference, that is, changing the variable that was passed in your call, without the need to return a value:

 scanf(" %f", &media);
 CalculaConceito(media, &conceito);
 printf("O conceito e %c !\n", conceito);
  • Oh yes, it’s just that the other code was like this, with the commercial

  • I edited the code there in the post, if you can see thank you Ruan

  • @CI2727 I edited the answer to supplement the question.

  • Let me see if I understand, so when it is of type int you can pass the parameter through and commercial? Now with float and int there is no way?

  • No. You can change any type of value by reference, the only problem was declaring the function (with float CalculaHora...) within the method main. If you take the float front of her call in function main and pass it to the beginning of the file, before the function declaration main, the code should work.

1

Notice, friend, that you are not calling the function: you are making a syntax error, and I’m sure you must have done it by mistake. Your code should be like this:

#include <stdio.h>

float CalculaHora(horas,minutos,segundos,*conversao);

int main() {
 float horas, minutos, segundos, conversao;
 printf("Digite a hora, minutos e segundos: ");
 scanf("%f %f %f", &horas,&minutos,&segundos);
 float horaCalculada = CalculaHora(horas,minutos,segundos,&conversao);
 printf("A quantidade de segundos eh %f !\n", conversao);
 system("pause");
 return 0;
}

Notice that I changed the line that the function was, now it is:

   float horaCalculada = CalculaHora(horas,minutos,segundos,&conversao);

Also, I declared the function before main, as it should be done after C-99.

  • Cool guy, I’ll test this code you posted, I’m without PC now, but I based on the code up there, edited my post

Browser other questions tagged

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