How to make variable receive function argument value?

Asked

Viewed 99 times

0

Next: I have a code for how to create a point structure on an Rxr plan. In this case, I would like to access the "x" value of a point. Follow the codes:

File point. h:

 //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);

File point. c:

#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 file. c:

#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;
}

Only when I do this, I get the message: error: Dereferencing Pointer to incomplete type.

What would be the correct way to do this? It would be using the pto_associa function?

Source of the code: https://programacaodescomplicada.wordpress.com/complementar/

  • Where the error message happens?

  • In row float X1 = p->x;

1 answer

0


When you try to access the member x of the struct p compiler needs to know what this struct looks like (for example, to check if there really is a member x in it). How did you not put the definition of type struct ponto anywhere where the compiler can see it when compiling main.c, you receive an error. The compiler does not know what a struct ponto contains.

If you put the definition of struct ponto in ponto.h,

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

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

Enter full type definition struct ponto in ponto.h, that’s where it should be.

  • 1

    Yeah. I ended up putting the structure in Ponto. c thinking it would be there. When I put it in Ponto. h, it worked. Thanks!

Browser other questions tagged

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