c编程变量读/写限制
在文件1中,我定义了一个变量int a=5,可以在文件中修改,我想访问文件2中变量a的值,但是我不能在这里修改值,例如a=10不起作用 C无法使变量仅在某些位置可写,但静态变量仅限于该源文件。因此,您可以只使用getter函数,将变量隐藏在所需的文件中c编程变量读/写限制,c,C,在文件1中,我定义了一个变量int a=5,可以在文件中修改,我想访问文件2中变量a的值,但是我不能在这里修改值,例如a=10不起作用 C无法使变量仅在某些位置可写,但静态变量仅限于该源文件。因此,您可以只使用getter函数,将变量隐藏在所需的文件中 // Header int get_a(); // Source file1 static int a; int get_a() { return a; } void foo() { a = 42; //OK } // S
// Header
int get_a();
// Source file1
static int a;
int get_a()
{
return a;
}
void foo()
{
a = 42; //OK
}
// Source file2
void bar()
{
int x = get_a(); // OK, function in header
a = x + 1; // Error, a was never declared in the header
}