0

Code:

#include <pthread.h>

void *thread(void* o) {
    return NULL;
}

void man(void)
{
    pthread_create(NULL, NULL, &***thread, NULL);
}

It compiles without warnings with -Wall -Wextra -Wpedantic. You can add more asterisks before thread, they don't have any effect. What was the reasoning between that? Why doesn't it give an error instead of being silent? Was this specified by C11?

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
MCCCS
  • 1,002
  • 3
  • 20
  • 44
  • 2
    `void man(void)`? – Eugene Sh. Jun 22 '18 at 15:17
  • @SeanBright I think the edit might have broken the question. `void main` should probably give warnings. – Eugene Sh. Jun 22 '18 at 15:19
  • @EugeneSh. I reverted that part of the edit. Apologies. – Sean Bright Jun 22 '18 at 15:19
  • 1
    https://stackoverflow.com/questions/7518815/function-pointer-automatic-dereferencing – pm100 Jun 22 '18 at 15:22
  • Assuming `int main(void)` I get two warnings: `unused parameter 'o'` and `null passed to a callee that requires a non-null argument` (the first argument to `pthread_create()`). Not sure how you compiled with no warnings. – Patrick Roberts Jun 22 '18 at 15:23
  • Short answer: backwards compatibility. Pre-C89/C90, a function pointer `foo` needed to be dereferenced before it could be called, e.g. `(*foo)(param1, param2);`, and it was necessary to take the address of a function to convert it to a pointer, e.g. `foo = &somefunction;`. In C89/C90 that was changed to make it easier for programmers who could now call a function pointer `foo` like `foo(param1, param2);` and could assign a function to a function pointer without using the address-of operator, e.g. `foo = somefunction;`. For backwards compatibility, `&` and `*` need to have no effect. – Ian Abbott Jun 22 '18 at 16:00
  • ... cont. However, applying multiple consecutive `&` to a function is not allowed. E.g. `foo = &&somefunction;` is not allowed, but `foo = &*&somefunction;` is allowed, as is `foo = *&****&*somefunction;`. – Ian Abbott Jun 22 '18 at 16:09
  • ...cont. So indeed `&` has _some_ effect as it produces an _rvalue_ and you cannot take the address of an _rvalue_. – Ian Abbott Jun 22 '18 at 16:17

0 Answers0