3

i read in The Linux Programming Interface 29-2 Threads and errno the next :

On Linux, a thread-specific errno is achieved in a similar manner to most other UNIX implementations: errno is defined as a macro that expands into a function call returning a modifiable lvalue that is distinct for each thread.

and i wondered how can a function return a modifiable lvalue.

Mohammad
  • 65
  • 7
  • 5
    A function cannot return a modifiable lvalue. But a function can return a pointer, and `errno` can be defined as a macro that expands to something like `*per_thread_errno_function()`. So the function does not return an lvalue, but the expansion of the `errno` macro is an lvalue. See also [this answer](https://stackoverflow.com/questions/46013418/how-to-check-the-value-of-errno/46014661#46014661). – Steve Summit Feb 26 '23 at 16:23

1 Answers1

7

It is the macro that "returns" the modifiable lvalue, not the function call itself. The function returns a pointer and the macro dereferences this pointer. For example, errno.h of the glibc source code defines the macro errno like this:

# define errno (*__errno_location ())

This wording is a bit misleading:

errno is defined as a macro that expands into a function call returning a modifiable lvalue that is distinct for each thread.

A more accurate wording would be:

errno is defined as a macro that expands into an expression that contains a function call. This expression evaluates to a modifiable lvalue that is distinct for each thread.

tink
  • 14,342
  • 4
  • 46
  • 50
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39