英文:
how can a function return an lvalue in c?
问题
在The Linux Programming Interface 29-2 Threads and errno中我读到以下内容:
"在Linux上,线程特定的errno是以与大多数其他UNIX实现类似的方式实现的:errno被定义为一个宏,它会展开成一个函数调用,返回一个可修改的lvalue,对于每个线程都是唯一的。"
我想知道如何让一个函数返回一个可修改的lvalue。
英文:
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.
答案1
得分: 7
这是一个宏,它“返回”可修改的左值,而不是函数调用本身。该函数返回一个指针,而宏则取消引用该指针。例如,glibc源代码中的errno.h
定义了宏errno
如下:
# define errno (*__errno_location ())
这种措辞有点误导:
errno
被定义为一个宏,它展开为一个函数调用,返回一个对于每个线程都是独立的可修改左值。
更准确的措辞应该是:
errno
被定义为一个宏,它展开为包含一个函数调用的表达式。这个表达式求值为对于每个线程都是独立的可修改左值。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论