Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can a function return an lvalue in c?

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.

like image 261
Mohammad Avatar asked Oct 14 '25 11:10

Mohammad


1 Answers

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.

like image 158
Andreas Wenzel Avatar answered Oct 17 '25 02:10

Andreas Wenzel