Your code has several errors. My advice, and what I consider most important, is to pay close attention to the warnings and errors that the compiler gives. Even the warnings are mostly (if not almost all) errors.
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’t be sizeof(int)
and yes sizeof(struct turma)
and the cast for (struct turma*)
, thus:
ptr_turma = (struct turma*)malloc(sizeof(struct turma)*100);
If you want to simplify do it first:
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
got really badly written, 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 exhaust Sequence: ' I'|
Has a \
the most. It should be \n\nInforme
instead of \n\n\Informe
.
scanf("%d", &ptr_turma[i].matricula);
main. c|30|Warning: format =%d' expects argument of type ːint *', but argument 2 has type .o.float *' [-Wformat=]
The guy from the field matricula
in the structure is float
, so the reading has to be with %f
and not %d
. The same applies to all other fields that are being read of the same type. I advise you to consider whether matricula
was supposed to be a real float
? For that seems strange to say the least.
main. c|30|Warning: voltar i' may be used uninitialized in this Function [-Wmaybe-uninitialized]
In addition to the type warning, also the variable i
was not initialized. Since it is an array the normal would initialize with 0
up there:
int i = 0; //<-----
char nome_turma[30];
Now we need to see what you intend to do with that i
, since I also don’t see any increments in the loop/loop. This means that your loop never leaves the same element, because the i
will never be reassigned.
printf("\n\Nota 1: ");
main. c|37|Warning: Unknown exhaust Sequence: ' N'|
Other \
that got the most of this, and should be:
printf("\nNota 1: ");
And with this everything should be without warnings and errors! (maybe?)
Why maybe ?
Because perhaps compiling code with a c++11 compiler 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 gets
is so dangerous and bad that it was actually removed in C++11, and as the warning indicates, it should not be used.
A good alternative in C (not C++) is fgets
, or even the scanf
if handled correctly.