0

I read such an example:

double * ptd;
ptd = (double *) malloc(30 * sizeof(double));

It explains that

malloc() allocates memory but it doesn't assign a name to it. However, it does return the address of the first byte of that block.

Since malloc return the the address of the first byte of that block, I assume the correct way is assign the address to ptd directly without star(*).

ptd = malloc(30 * sizeof(double));

The extra * would dereference the address.

What's wrong with my assumption?

AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
  • 1
    Which `*` are you referring to? Your statement is a bit unclear because it refers to "derefernce" but there is no deref in any of the `*` occurences shown. Both snippets of code will work. But arguably the second one is better as the `(double *)` cast is not needed. See [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – kaylum Jul 30 '20 at 01:02
  • * in (double *) ty, I got it. – AbstProcDo Jul 30 '20 at 01:15

0 Answers0