1

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.

Lex
  • 386
  • 1
  • 4
  • 20
  • possible duplicate of [Is typecast required in malloc?](http://stackoverflow.com/questions/4993327/is-typecast-required-in-malloc) – Lundin Jan 26 '12 at 07:54

1 Answers1

7

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.

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979