In the book "C Programming: A Modern Approach, chapter 17.4 introduced the function free()
which has the prototype of the following form: void free(void *ptr)
. As the author outlines several examples involving dynamic storage using malloc()
, the following snippet of quasi-code is encountered:
struct node {
int value;
struct node *next;
};
struct node *delete_from_list(struct node *list, int n)
{
struct node *cur, *prev;
.
. /*Will explain the relevant part of this section as it pertains to the question (bullet 3 below) */
.
free(cur); /* <--- my question is about this line */
}
The important points are:
- there is a data type known as
struct node
that has two fields - there is a pointer to
struct node
namedcur
- in the
delete_from_list
function,cur
will eventually get assigned some address in the heap. In particular, it will get assigned to a chunk of memory with data typestruct node
.
With those 3 points established, we can attend to my question: why does calling the function free()
with the argument cur
work without issue? cur
is not of type pointer to void. I assume that means there is some type of implicit (under the hood) type casting occurring that changes cur
's datatype from pointer to struct node
to pointer to void. Is that the case? If so, is there a general rule about implicit type casting of pointers as argument when it comes to functions calls?