double *p;
p = malloc(sizeof(p));
if (p != NULL)
{
*p = 5.15;
}
For some reason, p = malloc(sizeof(p));
doesn't work. I try to allocate as much memory as p
needs. What is wrong with that?
double *p;
p = malloc(sizeof(p));
if (p != NULL)
{
*p = 5.15;
}
For some reason, p = malloc(sizeof(p));
doesn't work. I try to allocate as much memory as p
needs. What is wrong with that?
I try to allocate as much memory as
p
needs.
p
itself (as a variable) has got the (own) memory allocated, what you're trying is basically allocate the memory for which p
will be pointing to.
Here, p
points to a double
, so it needs to have a memory area to be able to store a double
value. So, the allocation should be equal to the size of a double
, i.e,
p = malloc(sizeof*p);