Language C - function for Realloc struct pointer

Viewed 42

I'm trying to build a function that realloc a struct pointer (which works as an arrays of structs), but it is not working out:

I guess I'm not using the pointer reference correctly int the function cria_ponto, but I can't find out why.

TKS.

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

typedef struct ponto{
    float x, y;
} ponto;

ponto inicializa_vazio();
void cria_ponto(ponto *p, int *qntd);

int main(){
 
     int qntd_pontos = 0;
     ponto *p = NULL;
 
     cria_ponto(p, &qntd_pontos);
     printf("Coordenada x: %.2f, coordenada y: %.2f", p[0].x, p[0].y);
 
     return 0;
 
 }

 
 ponto inicializa_vazio(){
 
     ponto p;
     printf("Informe a coordenada X do ponto: ");
     scanf("%f", &p.x);
     printf("Informe a coordenada Y do ponto: ");
     scanf("%f", &p.y);
 
     return p;
};
 
 void cria_ponto(ponto *p, int *qntd){
        p = (ponto*) realloc(p, (*qntd+1)*sizeof(ponto));
        p[*qntd] = inicializa_vazio();
        *qntd = *qntd + 1;
};

0 Answers
Related