简体   繁体   English

在C中,从char *到void *的转换是否有作用?

[英]In C, does a cast from char* to void* do anything?

Please have a look at the below mentioned code snippet and tell me the difference? 请查看下面提到的代码片段,并告诉我其中的区别?

int main()
{
struct sockaddr_in serv_addr, cli_addr;
/* Initialize socket structure */
    bzero((char *) &serv_addr, sizeof(serv_addr));
}

Now, what if i do something similar without typecasting (char *) , then also i feel it will do the same thing? 现在,如果我在不进行类型转换(char *)情况下做类似的事情,那我也将做同样的事情吗? Can someone clarify? 有人可以澄清吗?

/* Initialize socket structure */
bzero( &serv_addr, sizeof(serv_addr));

Since the first parameter is void * , you only need to cast in C++. 由于第一个参数为void * ,因此只需要使用C ++进行转换。

In C this is not necessary, as a void * was introduced 1 precisely so that you wouldn't need to cast it to or from other object 2 pointers. 在C,这是没有必要的,为void *加入1正是如此,你会不会需要将其转换为或从其他对象2点的指针。 (Similarly with malloc() and other functions that deal with void * s) (与malloc()和其他处理void * s的函数类似)


  1. In C89. 在C89中。
  2. Any non-function pointer. 任何非功能指针。

不需要AnyType*转换,因为bzero()接受void*作为第一个参数,并且AnyType*可以隐式转换为void*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM