1

If 2 function definitions are:

    void func(struct Node *arg){...}

    void func2(void *arg){
      func(arg);
      ...
    }

but they are called like:

    struct Node *node = (char *)malloc(6);
    func2(node)

I think node is implictly casted to void* and then to struct Node*, so I don't need to do something like:

    func2((void *)node);
    or func((struct Node *)arg);

Is my understanding correct?

Mat
  • 202,337
  • 40
  • 393
  • 406
misteryes
  • 2,167
  • 4
  • 32
  • 58

1 Answers1

3
  1. There's no such thing as "implicit casting". There is explicit type conversion (casting) and implicit type conversion (type coercion or promotion).

  2. Since void * is compatible with any data pointer type (and if your implementation conforms to POSIX, then it's compatible with function pointers too), your assumption is right:

T *object = malloc(size);

is right regardless to the type T denotes. The same applies to function arguments, of course.

Some (myself included) even argue that casting in this case is dangerous, decreases readability and should be avoided.

Community
  • 1
  • 1