简体   繁体   English

c函数返回静态变量

[英]c function return static variable

I have a question about how C function returns static variable: 我有一个关于C函数如何返回静态变量的问题:

in data.h file: data.h文件中:

#include <stdio.h>
#include <stdlib.h> 

typedef struct
{
   int age;
   int number;
} person;

person * getPersonInfo();

in data.c data.c

#include "data.h"
static struct person* person_p = NULL;

person * getPersonInfo()
{
   person_p = (struct person*)malloc(10 * sizeof(struct person));
   return person_p;
}

in main.c main.c

#include "data.h"

int main()
{
   person* pointer = getPersonInfo();
   return 0;
}

function getPersonInfo() returns a pointer which is a static pointer in data.c , is this allowed and legal? function getPersonInfo()返回一个指针,它是data.c一个静态指针,这是允许的还是合法的? in the main.c , can the function getPersonInfo() be used like this: person* pointer = getPersonInfo(); main.c ,函数getPersonInfo()可以像这样使用: person* pointer = getPersonInfo();

Line 线

static struct person* person_p = NULL;  

declares a variable person_p which has static storage duration, file scope and internal linkage. 声明一个变量person_p ,它具有静态存储持续时间,文件范围和内部链接。 It means that it can be referenced by name only in file data.c and internal linkage restricts its sharing to this single file. 这意味着它只能通过文件data.c的名称引用,并且内部链接将其共享限制为此单个文件。

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. 静态存储持续时间意味着一旦将内存分配给person_p ,只要程序正在运行,它就会保持在同一存储位置,从而允许它无限期地保留其值。 It means you can return a pointer to that location. 这意味着您可以返回指向该位置的指针。
Therefore, your code is valid and legal. 因此,您的代码是有效且合法的。

person_p is a variable whose scope is just for the file data.c as it is a static variable. person_p是一个变量,其范围仅适用于文件data.c因为它是一个静态变量。 You are using this variable to allocate some memory on heap and return the address to main.c and there is a variable which holds this value in main.c ie pointer . 您正在使用此变量在堆上分配一些内存并将地址返回给main.c并且有一个变量在main.c中保存此值,即pointer

So here you are not returning the variable but the value the variable is holding which is totally fine because even if the variable goes out of scope the value returned(ie address) is still valid which is on heap. 所以这里你没有返回变量,而是变量持有的值,这是完全正常的,因为即使变量超出范围,返回的值(即地址)仍然有效,这在堆上。

It is legal, as already noted in the other answers. 这是合法的,正如其他答案中已经提到的那样。 You could even rewrite your data.c to this: 您甚至可以将data.c重写为:

#include "data.h"
static struct person person;

person * getPersonInfo()
{
   return &person;
}

This will return a pointer to your structure in data memory, without any need to allocate it first on the heap. 这将返回一个指向数据存储器中结构的指针,而不需要先在堆上分配它。 The static keyword is just about the scope of your symbol person (local to data.c) but the data itself is valid global in your program. static关键字只是符号person的范围(data.c的本地范围),但数据本身在程序中是有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM