Domanda

I have a header file, (lets call it gen.h) which contains the following line:

typedef void* pNode;
SampleFunction(PNode node); /* just a function for example*/

Now, lets say I have another source file (part.c), and it contains the following struct:

typdef struct _OBJ* POBJ;
typdef struct _OBJ
{
    double xi;
    double xf;
    double yi;
    double yf;
    int key;
} 

I want to send a pointer to the struct (PBOJ) as a parameter to a function (lets say SampleFunction) that is expecting to get a pointer to void type (pNode); so how do I do this?

È stato utile?

Soluzione

Below code is just a simple sample, so I did not write to init, delete, etc.

struct _OBJ
{
  double xi;
  double xf;
  double yi;
  double yf;
  int key;
}; 

typedef struct _OBJ* POBJ;

typedef void* pNode;

void SampleFunction(pNode node)
{
  //null check  
  if(node == NULL)
  {
    //error
  }

  //Do Something
  // ...

}; /* just a function for example*/

int main()
{
  POBJ obj = new _OBJ;
  SampleFunction((pNode)obj);
  delete obj;
}

Altri suggerimenti

You just cast it to PNode like any other type as void* can point to any other type safely.

Since PBOJ is a pointer to _OBJ you cast it directly without the & operator

POBJ Ptr;
SampleFunction((PNode)Ptr);
// or SampleFunction((void *)Ptr);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top