1

I saw on some online sources that they allocate a block of memory for an array of n integers as follows:

memory = (int*) malloc(n*sizeof(int))

What's the role of (int*) here?

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
Jack Shi
  • 131
  • 5
  • 2
    Nothing, it's a mistake that appeared in a popular 1970s book and people have been copying it ever since without thinking about it. `memory = malloc(n * sizeof(int));` or `memory = malloc(n * sizeof *memory);` is correct. The latter has the advantage that you cannot accidentally name the wrong type and thereby allocate the wrong amount of memory. – M.M Nov 14 '15 at 06:38

0 Answers0