如何在C中使一个函数返回一个左值?

huangapple go评论102阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年2月27日 00:12:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定