Expected Primary-Expression before"int" - C

Asked

Viewed 274 times

0

I have the following error in my code: expected primary-expression before"int"

 #include<stdio.h>
        #include<stdlib.h>

        void imc(float massa_corporal[3], float peso[3], float altura[3], int i){
            for (i=0; i<3; i++){
            printf ("\nInforme seu peso: ");
            scanf ("%f", &peso[i]);

            printf ("Informe sua altura: ");
            scanf ("%f", &altura[i]);

            massa_corporal[i]= peso[i] / (altura[i]*altura[i]); 
            }

            for (i=0; i<3; i++){
            printf ("imc[%d]= %f\n",i , massa_corporal[i]);
            }
        }

        int main(){
            float massa_corporal[3], peso[3], altura[3];
            int i;
            imc (massa_corporal[3], peso[3], altura[3], int i);
        }
  • Which line is the error in?

  • line 25, in the call of the IMC function.

  • Just take the int in the function call. Now: what is the sense of you passing as a parameter a variable that will only be used in an auxiliary way (only an index) in the body of the function? You have to call the function of the form: imc (massa_corporal, weight, height, i); otherwise you will be referring to a specific item of the array, which, by the way, will be outside the limits of the array.

  • Comment at the end of the line with error // erro aqui, pq does not even have 25 lines of code in this excerpt.

1 answer

0


The problem is that you are making confusion between vector declaration and vector passage as parameter.

When you declare the vector with float massa_corporal[3], peso[3], altura[3]; the program understands that you are creating 3 vectors of 3 positions each.

However, when you send to the one function massa_corporal[3] is trying to pass specifically the 4th position, which does not exist (remember that your vector has the position 0, 1 and 2, only). Thus, the correct is to omit this redeclaration when calling the function.

Similarly you can remove the size specification in the function call. That being said, the correct is:

#include<stdio.h>
#include<stdlib.h>

void imc(float massa_corporal[], float peso[], float altura[], int i){
    for (i=0; i<3; i++){
    printf ("\nInforme seu peso: ");
    scanf ("%f", &peso[i]);

    printf ("Informe sua altura: ");
    scanf ("%f", &altura[i]);

    massa_corporal[i]= peso[i] / (altura[i]*altura[i]); 
    }

    for (i=0; i<3; i++){
    printf ("imc[%d]= %f\n",i , massa_corporal[i]);
    }
}

int main (){
    float massa_corporal[3], peso[3], altura[3];
    int i;
    imc (massa_corporal, peso, altura, i);
}
  • 1

    Thank you very much, now I understand, I was a little lost in relation to the use of functions.

Browser other questions tagged

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