从“无效*”为指针,以非'void“转换需要显式转换(第17行) [英] Conversion from 'void*' to pointer to non-'void' requires an explicit cast (Line 17)

查看:130
本文介绍了从“无效*”为指针,以非'void“转换需要显式转换(第17行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照书学习C坚硬方式,当我尝试运行这个程序,我收到此错误信息:


  

这是'无效*'的指针的非'void转换需要显式的转换。


我不知道如何解决这个问题,我必须改变返回变量的结构?

看看反正这里的code:(编译基于Visual C ++ 2010,没有尝试过GCC还)

  //学习C的在Hardway  #包括LT&;&ASSERT.H GT;
  #包括LT&;&stdlib.h中GT;
  #包括LT&;&string.h中GT;
  #包括LT&;&stdio.h中GT;  结构{人
      字符*名称;
      INT年龄;
      INT高度;
      诠释权重;
  };  结构人* Person_create(字符*名称,INT年龄,身高INT,INT重量)
  {
      结构人*谁=的malloc(sizeof的(结构人));
      断言(谁!= NULL);      如─>名称=的strdup(名);
      如─>年龄=年龄;
      如─>高度=高度;
      如─>体重=体重;      返回谁;
  }  无效Person_destroy(结构人*谁)
  {
      断言(谁!= NULL);      免费(如 - >名);
      免费的(谁);
  }  无效Person_print(结构人*谁)
  {
      的printf(名称:%s \\ n,如─>名);
      的printf(\\踏歌数:%d \\ n,如─>年龄);
      的printf(\\ tHeight数:%d \\ n,如─>高度);
      的printf(\\ tWeight数:%d \\ n,如─>重);
  }  INT主(INT ARGC,CHAR *的argv [])
  {
      //让两个人结构
      结构人*乔= Person_create(
              乔亚历克斯,32,64,140);      结构人*坦诚= Person_create(
              弗兰克空白,20,72,180);      //打印出来,他们是在内存中
      的printf(乔是在内存位置%P:\\ n,JOE);
      Person_print(JOE);      的printf(弗兰克在内存位置%P:\\ n,坦率的);
      Person_print(坦诚);      //让每个人都20岁,再打印出来
      joe->年龄+ = 20;
      joe->高度 - = 2;
      joe->重+ = 40;
      Person_print(JOE);      frank->年龄+ = 20;
      frank->重+ = 20;
      Person_print(坦诚);      //消灭他们两个,所以我们清理
      Person_destroy(JOE);
      Person_destroy(坦诚);      返回0;
  }


解决方案

这行需要转换:

 结构人*谁=的malloc(sizeof的(结构人));

应该是:

 结构人*谁=(结构人*)malloc的(的sizeof(结构人));

这是唯一的,因为你正在编译此code和C ++,而不是C.在C,中投是不必要的,并为你做的隐含

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

解决方案

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.

这篇关于从“无效*”为指针,以非'void“转换需要显式转换(第17行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆