How to make variable receive function argument value?

0

Next: I have a code for how to create a one-point structure in an RxR plane. In case, I would like to access the "x" value of a point. Here are the codes:

Point.h file:

 //Arquivo Ponto.h
 typedef struct ponto Ponto;

 //Cria um novo ponto
 Ponto* pto_cria(float x, float y);

 //Libera um ponto
 void pto_libera(Ponto* p);

 //Acessa os valores "x" e "y" de um ponto
 int pto_acessa(Ponto* p, float* x, float* y);

 //Atribui os valores "x" e "y" a um ponto
 int pto_atribui(Ponto* p, float x, float y);

 //Calcula a distância entre dois pontos
 float pto_distancia(Ponto* p1, Ponto* p2);

Point.c file:

#include <stdlib.h>
#include <math.h>
#include "Ponto.h" //inclui os Protótipos

//Definição do tipo de dados
  struct ponto{
    float x;
    float y;
  };

//Aloca e retorna um ponto com coordenadas "x" e "y"
Ponto* pto_cria(float x, float y){
       Ponto* p = (Ponto*) malloc(sizeof(Ponto));
       if(p != NULL){
           p->x = x;
           p->y = y;
       }
       return p;
 }

 //Libera a memória alocada para um ponto
 void pto_libera(Ponto* p){
         free(p);
 }

 //Recupera, por referência, o valor de um ponto
 int pto_acessa(Ponto* p, float* x, float* y){
      if(p == NULL)
           return 0;
      *x = p->x;
      *y = p->y;
      return 1;
 }

 //Atribui a um ponto as coordenadas "x" e "y"
 int pto_atribui(Ponto* p, float x, float y){
      if(p == NULL)
         return 0;
      p->x = x;
      p->y = y;
      return 1;
 }

//Calcula a distância entre dois pontos
float pto_distancia(Ponto* p1, Ponto* p2){
      if(p1 == NULL || p2 == NULL)
           return -1;
      float dx = p1->x - p2->x;
      float dy = p1->y - p2->y;
      return sqrt(dx * dx + dy * dy);
 }

Main.c file:

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

int main(){
   float d;
   Ponto *p,*q;

   p = pto_cria(10,21);
   q = pto_cria(7,25);

   //Aqui, quero que a variável x1 receba o valor "x" de p
   float x1 = p->x;


   d = pto_distancia(p,q);


   printf("Ponto 1: %f\n", x1);
   printf("Distancia entre pontos: %f\n", d);

   pto_libera(q);
   pto_libera(p);

   system("pause");
   return 0;
}

But when I do this, I get the message: error: dereferencing pointer to incomplete type.

What would be the right way to do this? Would you be using the pto_associa function?

Source code: link

    
asked by anonymous 11.06.2017 / 14:41

1 answer

0
When you try to access the member x of struct p the compiler needs to know how this struct looks (for example, to check if any x member exists inside it). Since you did not set the struct ponto setting anywhere where the compiler can see it when compiling main.c , you get an error. The compiler does not know what a struct ponto contains.

If you set the struct ponto setting to ponto.h ,

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

The compiler can see how this type is structured and the struct members can be used.

Enter the full definition of type struct ponto into ponto.h , that's where it should be.

    
11.06.2017 / 16:41