简体   繁体   中英

what does (int *) before malloc mean?

what does the (int *) do in the following code?

int *ptr = (int *) malloc(10 * sizeof (int));

i'm new to C and i've seen the above code with and without the (int *) so im wondering what it does.

That means "cast a void* pointer into a int* pointer" - malloc() returns void* and you ask the compiler to treat that void* as if it was int* . This construct around malloc() is only needed in C++ code, and is totally unneeded and even evil in C because it can cause rather subtle yet devastating errors.

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