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 :(.
d
is a array of the kindfacu
, defined by struct; in the function you expect a array of int, but passes the value of an attributemat
ofd
, which is a array? How confusing. What I really wanted to do?– Woss
I WANT TO PASS THE VALUES STORED IN THE ARRAY TO THE FUNCTION
– Graciano Souza
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.
– Woss
I edited it, thanks for the tips
– Graciano Souza
There is no integer vector being passed. The code must have several build warnings, and running to the end is a coincidence
– Jefferson Quesado