Problem passing array as parameter

Asked

Viewed 79 times

0

I am unable to pass the array parameters correctly to the function calculos. The code compiles correctly, but the function does not receive the values I am trying to pass. Since it is an array of the struct type, there is the problem.

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

typedef struct faculdade
{
  char disc[40];
  int mat;
  int rfreq;
  int arpro;
} facu;


void calculos(int quant[],int aprov[],int repr[],float *por_apr,float *por_re)
{ int i;



    int total=0;
    int total_apro=0;
    int total_re=0;

    for(i=0;i<1;i++){
    total+=quant[i];
    total_apro+=aprov[i];
    total_re+=repr[i];}
    *por_apr=((float)total_apro/total)*100;
    *por_re=((float)total_re/total)*100;



}








int main(){
  facu d[100];
  int i,n=1;
  float p_ap,p_re;

    for (i=0; i<n; i++){
      printf("Digite a %d° disciplina:\n",i+1);
      scanf("\n%[^\n]", d[i].disc );
      printf("digite quantos alunos foram matriculados na disciplina %s:\n",d[i]);
      scanf("%d", &d[i].mat );
      printf("digite quantos alunos foram aprovados na disciplina %s:\n",d[i]);
      scanf("%d", &d[i].arpro );
      printf("digite quantos alunos foram reprovados na disciplina %s:\n",d[i]);
      scanf("%d", &d[i].rfreq );
    }for (i=0; i<n; i++){
    printf("\nmatriculados %d\n",d[i].mat);
    printf("aprovados %d\n",d[i].arpro);}

    calculos(d->mat,d->arpro,d->rfreq,&p_ap,&p_re);







  return 0;

}
  • 1

    d is a array of the kind facu, defined by struct; in the function you expect a array of int, but passes the value of an attribute mat of d, which is a array? How confusing. What I really wanted to do?

  • I WANT TO PASS THE VALUES STORED IN THE ARRAY TO THE FUNCTION

  • 1

    So please edit the question and be clearer about your need. Explain with texts what the code should do and what it is doing at the moment. If you’re giving an error message, put in the message as well. Take the [tour] to learn how the site works and read the [Ask] guide. Remember to turn off the caps lock.

  • I edited it, thanks for the tips

  • There is no integer vector being passed. The code must have several build warnings, and running to the end is a coincidence

1 answer

0


Guy, I kind of misunderstood what you want with this function, so if you can explain to me I’m sure I can improve my answer to your better understanding, but going straight to the answer.

First pass your struct direct in the parameters since you are working with it anyway, like this void calculos ( facu* dado, int tam, float* por_apr, float* por_re ), it is very important to note that when you pass a vector per parameter you have to pass the size of it, because otherwise the function will not know what it is, and this can result in errors, vectors can be passed in parameters in two ways, following the example above, as facu* dado or facu dado[], both are the same thing only that in one the pointer is implicit.

It is important that since you do not know what will be the size of the vector, ask the user, so allot a vector of correct size, this way you have a gain in performance.

To make a dynamic allocation just have the library <stdlib.h> included and then declare a pointer to the type you want to allocate then call the function malloc, ex: facu* d = (facu*)malloc(sizeof(facu) * tam);, it works as follows, in the parameters of the malloc you will pass the size of the data type you are using (very recommended to use sizeof, since the size can vary from architecture to architecture) and the size you want your vector to have, it is very important to remember that you should always make a casting for the type of pointer you are using, so the (facu*), and also always remember when you finish using vector free the space allocated in memory with the command free, ex: free(d);.

You also had a little mistake in your first scanf, scanf("\n%[^\n]", d[i].disc );, I don’t know if it was on purpose or not, but don’t do it erase this \n, and always remember to leave a space between the quotation marks and the %.. because it avoids mistakes of buffers, anyway form the way I consider it correct to do this scanf is, scanf(" %[^\n]s", d[i].disc );.

Without further ado here is the answer (I think).

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

typedef struct faculdade
{
  char disc[40];
  int mat;
  int rfreq;
  int arpro;
} facu;

void calculos ( facu* dado, int tam, float* por_apr, float* por_re )
{
    int i, total=0, total_apro=0, total_re=0;

    for ( i = 0; i < tam; ++i) {
        total += dado[i].mat;
        total_apro += dado[i].arpro;
        total_re += dado[i].rfreq;
    }

    *por_apr = ((float)(total_apro/total)) * 100;
    *por_re = ((float)(total_re/total)) * 100;
}

int main()
{
  int i, tam; /** declare a variavel do tamanho aqui. */
  float p_ap, p_re;

  printf("Quatos alunos ha: "); /* pergunte o tamanho do vetor, depois aloque-o */
  scanf(" %i", &tam);
  facu* d = (facu*)malloc(sizeof(facu) * tam);

    for ( i = 0 ; i < tam; ++i ) {
      printf("Digite a %i° disciplina: ", i+1);
      scanf(" %[^\n]s", d[i].disc );

      printf("digite quantos alunos foram matriculados na disciplina %s: ", d[i].disc);
      scanf(" %i", &d[i].mat );

      printf("digite quantos alunos foram aprovados na disciplina %s: ", d[i].disc);
      scanf(" %i", &d[i].arpro );

      printf("digite quantos alunos foram reprovados na disciplina %s: ", d[i].disc);
      scanf(" %i", &d[i].rfreq );
    }

    for ( i = 0; i < tam; ++i ) {
    printf("\nmatriculados %i\n", d[i].mat);
    printf("aprovados %i\n", d[i].arpro);
    }

    calculos(d, tam, &p_ap, &p_re);

    free(d);        

    return 0;

}

For a better understanding of what was said here are some links that can help you:

Reference passage:

http://www.cprogressivo.net/2013/03/Variaveis-apontadas-A-passagem-por-Referencia-em-C.html

Dynamic allocation of memory:

http://www.cprogressivo.net/p/alocacao-dinamica-de-memoria-em-c.html

It had more links, but unfortunately I could not post :(.

  • Thank you very much man, I’m kind of starting data structures in college right now, and I’m still having a little bit of trouble, there are other problems in this exercise, but my main problem was this, you gave a good whitening, thank you very much.

  • 1

    Missed free(d); before the return 0;

  • yes, suashaushauhs, always forget, but it has been edited and added there, thank you very much for the remark.

  • 1

    Attention that the printf of Plina is not right, here: printf("digite quantos alunos foram matriculados na disciplina %s: ", d[i]);. Should be d[i].disc. This error exists in the various impressions of the discipline

  • thx bro, I hadn’t even noticed that.

Browser other questions tagged

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