0

I have a function that will return a void pointer. I need to cast it as a struct variable. For example, the struct is

struct book{
  char* name;
  int pages;
}

I already have assigned a local pointer to the struct, like this:

struct book* ptr;

The function returns a void pointer, and I need to cast it as a book pointer. Could I do this?

ptr = function_name(); //void pointer returned

Or would I have to "catch" the returned void pointer in a local void pointer, then convert that to ptr?

Thanks!

Clifford
  • 82,791
  • 12
  • 81
  • 153

1 Answers1

0
struct book{
  char* name;
  int pages;
};

A void* ptr does not need an explicit cast.(although for compatibility or compiler issues you might need to explicitly cast as
struct book* ptr = (struct book*)ptr = (struct book*)function_name();

struct book *ptr = function_name();
shirish
  • 668
  • 4
  • 9