error: cannot Convert 'int*' to 'main()::class*' in assignment

Asked

Viewed 190 times

0

I am trying to compile my code and presents the error reported above

error: cannot Convert 'int*' to 'main()::class*' in assignment"

on line 21 (ptr_turma =(int*)malloc(sizeof(int)*100);)

Follows the code:

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

int main(void){

   struct turma {
        float matricula;
        char nome_aluno[30];
        float nota1;
        float nota2;
        float nota3;

    };
    int  i;
    char nome_turma[30];


    struct turma Turma, *ptr_turma;
    ptr_turma = &Turma;
    ptr_turma =(int*)malloc(sizeof(int)*100);

    printf (">> Calculo de notas <<");
    printf ("\n\n Informe o nome da turma: ");
    gets (nome_turma);
    printf ("\n\n\Informe os dados solicitados dos alunos (digite -1 na matricula para sair)...");

    while(prt_turma[i].matricula == -1){
        printf("\nMatricula: ");
        scanf("%d", &ptr_turma[i].matricula);
            if(ptr_turma[i].matricula == -1){
                    break;
            }
        fflush(stdin);
        printf("\nNome: ");
        gets(ptr_turma[i].nome_aluno);
        printf("\n\Nota 1: ");
        scanf("%d", &ptr_turma[i].nota1);
        printf("\nNota 2: ");
        scanf("%d", &ptr_turma[i].nota2);
        printf("\nNota 3: ");
        scanf("%d", &ptr_turma[i].nota3);
    }

}

2 answers

3

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.

0

Colleague, copying and pasting your code, I found that there is problem on line 28, where it is a matter of correcting the spelling.

  typedef struct turma {
    float matricula;
    char nome_aluno[30];
    float nota1;
    float nota2;
    float nota3;

} Turma;

I made this adjustment. And then this:

 Turma *ptr_turma;

while(ptr_turma[i].matricula== -1){

The correction I quoted early on.

There are some adjustments to work the way you want. More this way is running. I hope I’ve helped.

Browser other questions tagged

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