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