I have a question regarding pointer initialization in C.
I understand that *ptr will give the value of that pointer is pointing to.
ptr will give you the address.
Now I got following syntax:
int *ptr = (int *) malloc(sizeof(*ptr));
Why is *ptr being initialized with an address of the Heap and not a value? malloc() returns an address right?
Shouldn't it be:
int *ptr;
ptr = malloc(...);
int *(pointer toint) fromptr = (int *) malloc(sizeof(*ptr));and you have your answer.*is context-sensitive. It means "pointer to" in one context, "pointer dereference" in another and "multiplication" as well.void-pointers in C.malloc(and such a cast could actually lead to bugs).int *ptr=> ptr is a variable that deferenced with*gives anint.