error: can not convert 'int *' to 'main () :: class *' in assignment

0

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

  

error: can not convert 'int *' to 'main () :: class *' in assignment '

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

Follow 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);
    }

}
    
asked by anonymous 02.05.2018 / 03:22

2 answers

3

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.

    
02.05.2018 / 04:55
0

Colleague, copying and pasting your code, I noticed that there is a problem on line 28, where it's 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 this one:

 Turma *ptr_turma;

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

The correction that I quoted at the beginning.

Make some adjustments to work the way you want. More like this is running. I hope I have helped.

    
02.05.2018 / 04:55