且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

c函数返回静态变量

更新时间:2022-06-09 23:29:35

static struct person* person_p = NULL;  

声明一个变量 person_p 具有静态存储时间,档案范围和内部链接。这意味着,它可以通过名称只在文件中引用 data.c 和内部连接限制了其分享到这一单个文件。

declares a variable person_p which has static storage duration, file scope and internal linkage. It means that it can be referenced by name only in file data.c and internal linkage restricts its sharing to this single file.

静态存储时间意味着一旦内存分配给 person_p 只要程序正在运行,将保持在同一存储位置,允许其无限期地保留它的价值。这意味着你可以返回一个指向该位置。结果
因此,您的code是有效和合法。

Static storage duration means once the memory is allocated to person_p it will stay at the same storage location as long as program is running, allowing it to retain its value indefinitely. It means you can return a pointer to that location.
Therefore, your code is valid and legal.