TheBlueCat
TheBlueCat

Reputation: 1195

Conversion from 'void*' to pointer to non-'void' requires an explicit cast (Line 17)

I'm following the book Learn C the Hard Way and when I try to run this program I get this error message:

Conversion from 'void*' to pointer to non-'void' requires an explicit cast.

I'm not sure how to resolve this, do I have to change the return variable in the struct?

Take a look anyway, here the code: (Compiling on Visual C++ 2010, haven't tried GCC yet).

   //learn c the hardway

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

  struct Person {
      char *name;
      int age;
      int height;
      int weight; 
  };

  struct Person *Person_create(char *name, int age, int height, int weight) 
  {
      struct Person *who = malloc(sizeof(struct Person)); 
      assert(who != NULL);

      who->name = strdup(name);
      who->age = age; 
      who->height = height;
      who->weight = weight;

      return who;
  } 

  void Person_destroy(struct Person *who)
  {
      assert(who != NULL);

      free(who->name);
      free(who);
  }

  void Person_print(struct Person *who) 
  {
      printf("Name: %s\n", who->name);
      printf("\tAge: %d\n", who->age); 
      printf("\tHeight: %d\n", who->height);
      printf("\tWeight: %d\n", who->weight); 
  }

  int main(int argc, char *argv[])
  {
      // make two people structures 
      struct Person *joe = Person_create(
              "Joe Alex", 32, 64, 140);

      struct Person *frank = Person_create(
              "Frank Blank", 20, 72, 180); 

      // print them out and where they are in memory 
      printf("Joe is at memory location %p:\n", joe);
      Person_print(joe);

      printf("Frank is at memory location %p:\n", frank);
      Person_print(frank); 

      // make everyone age 20 years and print them again 
      joe->age += 20;
      joe->height -= 2;
      joe->weight += 40; 
      Person_print(joe);

      frank->age += 20;
      frank->weight += 20; 
      Person_print(frank);

      // destroy them both so we clean up 
      Person_destroy(joe);
      Person_destroy(frank);

      return 0;
  }

Upvotes: 1

Views: 8254

Answers (4)

Mr Lister
Mr Lister

Reputation: 46629

The error message is caused by the fact that C doesn't require such a conversion explicitly, while C++ does. Try to make sure that the compiler treats the source as C rather than C++.

Upvotes: 0

James McNellis
James McNellis

Reputation: 355357

The Visual C++ compiler will attempt to determine the language being compiled from the file extension of the source file being compiled. For example, a file with a .cpp extension is compiled as C++, and a file with a .c extension is compiled as C.

Your program appears to be valid C, but not valid C++: in C, the void* to T* conversion is implicit; in C++ a cast is required.

If you want the compiler to compile it as C, you either need to change its file extension, or pass the /TC switch to the compiler to tell it to compile the file as C.

Upvotes: 5

ouah
ouah

Reputation: 145919

struct Person *who = malloc(sizeof(struct Person)); 

This needs to be casted in C++:

struct Person *who = (struct Person *) malloc(sizeof(struct Person)); 

In C the cast is not necessary because there is an implicit conversion from void * to any object pointer type. This implicit conversion is not present in C++, so the cast is required in C++.

Upvotes: 3

Reed Copsey
Reed Copsey

Reputation: 564891

This line requires a cast:

  struct Person *who = malloc(sizeof(struct Person)); 

Should be:

  struct Person *who = (struct Person *)malloc(sizeof(struct Person)); 

This is only because you're compiling this code as C++, and not C. In C, the cast is unneeded, and implicitly done for you.

Upvotes: 12

Related Questions