简体   繁体   中英

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? 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++.

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. (Similarly with malloc() and other functions that deal with void * s)


  1. In C89.
  2. Any non-function pointer.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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