在c语言中,有没有一种方法可以在不使用全局变量或模块变量的情况下持久地存储值?
例如,如果我编写了一个名为add的ftn,它使用int a并生成a+b的值,那么b是最后一个调用的ftn生成的值。 乙二醇在c语言中,有没有一种方法可以在不使用全局变量或模块变量的情况下持久地存储值?,c,C,例如,如果我编写了一个名为add的ftn,它使用int a并生成a+b的值,那么b是最后一个调用的ftn生成的值。 乙二醇 编辑:即使不使用静态静态局部变量如何 int add(int a) { static int b = 0; b += a; return b; } 或者可能是一个兰姆达: int main(int,char**) { int b = 0; auto add = [&](int a) { b += a; return b; }
编辑:即使不使用静态静态局部变量如何
int add(int a)
{
static int b = 0;
b += a;
return b;
}
或者可能是一个兰姆达:
int main(int,char**)
{
int b = 0;
auto add = [&](int a) { b += a; return b; };
std::cout << add(1) << "\n";
std::cout << add(2) << "\n";
std::cout << add(4) << "\n";
return 0;
}
这能满足你的需要吗
int add(int a) {
static int b = 0;
return b += a;
}
静态变量b是add函数的局部变量,但在调用之间保持其值。为什么没有静态变量?这正是你需要的,不是吗? 否则,您可以将参数作为指针传递,并在任何地方更改它
void f( int * a )
{
*a += 5;
}
在主要的地方,或者你打电话到的任何地方
int a = 0;
f( &a );
f( &a );
a将是10,你也可以使用它的功能
这有帮助吗?请考虑:
#include <stdio.h>
int test1(){
static int localVar=0;
localVar++;
printf("test1 %i\n",localVar);
}
int test2(){
static int localVar=0;
localVar++;
printf("test2 %i\n",localVar);
}
int main(){
test1();
test2();
test1();
}
因为static应用于定义它的范围内的变量,所以在本例中,test1和test2内的变量都以值0开始,并且每次调用函数时都会递增,正如您所看到的,即使在第一次调用test1之后,test2内的值也没有递增。再次调用test1表明该值已被记住。如果您不确定,请调整每次调用的次数,然后自己尝试 如果需要维护状态,但不希望函数将其存储在静态变量中,则有有限的选项 你可以通过考试,例如:
/* Be careful with the first call - the caller must initialise
previous_result to 0 */
int add(int a, int *prev_result)
{
int result = a + *prev_result;
*prev_result = result;
return result;
}
或者,您可以将其存储在文件中,例如:
/* Incomplete, and completely untested! */
int add(int a)
{
FILE *state_file;
int result
int prev_result;
state_file = fopen("state.txt", "r");
prev_result = get_value_from_file(state_file); /* write this yourself */
fclose(state_file);
result = a + prev_result;
state_file = fopen("state.txt", "w");
write_value_to_file(state_file, result); /* write this yourself */
fclose(state_file);
return result;
}
静态局部变量如何?我相信您正在函数中寻找变量前面的static关键字?static不是模块范围吗?@Defective module?,有四件事:1局部静态变量范围是它声明的函数的局部,例如下面的add function中,因此所有内容都不同,但在你的计划没有终止之前,生命是存在的。3所有的初始值默认为0。和4个来自静态段的内存。为什么要这样做?如果你想让程序记住上一次函数调用的值,该值必须存储在某个地方。即使没有static,你知道吗?即使没有static,你有什么想法吗?@Unfacture阅读Michal Bukovy使用通过地址指针的解决方案。@Unfacture:我添加了一个使用lambda的示例。为了良好的形式,我也会将这些localvars初始化为零。好的一点,GCC非常可靠地照顾你,但没有gaurantee,我会解决。谢谢。对于这个问题,我只是出于好奇void add int*a,int-value{*a+=value;printf%d\n,*a;}并调用int-a=0;增加&a,一些价值;
test1 1
test2 1
test1 2
/* Be careful with the first call - the caller must initialise
previous_result to 0 */
int add(int a, int *prev_result)
{
int result = a + *prev_result;
*prev_result = result;
return result;
}
/* Incomplete, and completely untested! */
int add(int a)
{
FILE *state_file;
int result
int prev_result;
state_file = fopen("state.txt", "r");
prev_result = get_value_from_file(state_file); /* write this yourself */
fclose(state_file);
result = a + prev_result;
state_file = fopen("state.txt", "w");
write_value_to_file(state_file, result); /* write this yourself */
fclose(state_file);
return result;
}
#include <stdio.h>
int add(int value){
FILE *fp;
int pv = 0;
fp = fopen("temp.bin", "r+b");
if(fp==NULL){
fp = fopen("temp.bin", "wb+");
fwrite(&pv, sizeof(int), 1, fp);
rewind(fp);
}
fread(&pv, sizeof(int), 1, fp);
pv += value;
rewind(fp);
fwrite(&pv, sizeof(int), 1, fp);
fclose(fp);
return pv;
}
int main() {
printf("%d\n", add(1));
printf("%d\n", add(2));
printf("%d\n", add(4));
return 0;
}