Your code has several errors. My advice, and what I consider more important is to take extreme care of the warnings and errors that the compiler gives. Even the warnings are mostly (if not almost all the same) mistakes.
Let's go through them all, one by one:
-
ptr_turma =(int*)malloc(sizeof(int)*100);
main.c | 21 | warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] |
If you want to create an array of turma
then it can not be sizeof(int)
and yes sizeof(struct turma)
and cast to (struct turma*)
, like this:
ptr_turma = (struct turma*)malloc(sizeof(struct turma)*100);
If you want to simplify do before:
ptr_turma = malloc(sizeof(struct turma)*100);
That conversion is implicit.
-
while(prt_turma[i].matricula == -1){
main.c | 28 | error: 'prt_turma' undeclared (first use in this function) |
Here prt_turma
was really misspelled, and should be ptr_turma
.
-
printf ("\n\n\Informe os dados solicitados dos alunos (digite -1 na matricula para sair)...");
main.c | 26 | warning: unknown escape sequence: '\ I' |
You have an extra%%. It should be \
instead of \n\nInforme
.
-
\n\n\Informe
main.c | 30 | warning: format '% d' expects argument of type 'int *', but argument 2 has type 'float *' [-Wformat =]
The type of field scanf("%d", &ptr_turma[i].matricula);
in the structure is matricula
, so the read must be with float
and not %f
. The same applies to all other fields that are being read of the same type. Do I advise you to consider whether %d
was supposed to be matricula
? For that seems at least odd.
main.c | 30 | warning: 'i' may be used uninitialized in this function [-Wmaybe-uninitialized]
In addition to the type warning, the float
variable has also not been initialized. Since it is an array the normal would be to initialize with i
there at the top:
int i = 0; //<-----
char nome_turma[30];
Now you have to see what you want to do with this 0
, since I also do not see any loop / cycle increment. This means that your loop never exits the same element, because i
is never re-assigned.
-
i
main.c | 37 | warning: unknown escape sequence: '\ N' |
Another printf("\n\Nota 1: ");
that stayed the most in this, and should be:
printf("\nNota 1: ");
And with this everything should be without warning and errors! (maybe?)
Why maybe?
Because maybe compiling the code with a c ++ compiler 11 will actually see two warnings:
main.c | 24 | warning: implicit declaration of function 'gets' [-Wimplicit-function-declaration] |
main.c | 24 | warning: the 'gets' function is dangerous and should not be used.
It turns out that the \
function is so dangerous and bad, that it was actually removed in C ++ 11, and as the warning itself indicates, should not be used.
A good alternative in C (and not C ++) is gets
, or even the fgets
if handled correctly.