指向函数内静态变量的指针。 C
Pointer to a static variable inside a function. C
我的目录中有 3 个文件:
ex22.h、ex22.c。 ex22_main.c
ex22.h:
#ifndef _ex22_h
#define _ex22_h
extern double* v;
...
#endif
ex22.c:
#include <stdio.h>
#include "ex22.h"
double* v;
/*a function that accepts a new_variable, changes a
static variable inside of it while keeping track of
what the static variable was before the change. The
purpose of the function is to update the static
variable and print what it was right before the
updating.*/
double update_variable(double new_variable)
{
static double variable = 1.0;
double old_variable = variable;
variable = new_variable;
v = &variable;
return old_variable;
}
...
ex22_main.c:
#include "ex22.h"
#include "dbg.h"
...
int main(void)
{
//test if it is possible
printf("Variable at first: %f", update_variable(2.0);
printf("Access 'variable' inside update_variable: %f", *v);
}
Compiller (ubuntu) 给我这些错误信息:
cc ex22_main.c -o ex22_main
/tmp/ccGLFiXP.o: In function `main':
...
ex22_main.c:(.text+0x1f4): undefined reference to `update_variable'
ex22_main.c:(.text+0x222): undefined reference to `r'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'ex22_main' failed
make: *** [ex22_main] Error 1
希望您理解我想要实现的目标。我的目标是通过指向它的指针来访问函数内部的静态变量(这是不可能的)。我只是想知道它是不是那样工作的?
EDIT:
我的代码中有一些愚蠢的错误,但通过指针访问函数内部静态变量的想法是完全可行的。
避免我的错误:
1) 确保文件已链接
2) 注意变量名
编译器,或者更准确地说是 linker,找不到 update_variable
或 r
的定义,因为你没有't 编译和 link ex22.c 其中包含这些定义。
您需要编译这两个文件并将它们 link 在一起。
cc -o ex22_main ex22_main.c ex22.c
My goal is to access the static variable inside a function (which is not possible) by making a pointer to it. I'm just curious if it works that way?
是的,确实如此。 static
对于在函数内部声明的变量与在文件范围内声明的变量具有不同的含义,但是这两种变体都存在并且在程序的整个生命周期内具有相同的地址。指向此类变量的指针可用于在程序中的任何位置访问其值,而不管该变量是否可以通过其在访问指针时声明的标识符进行访问。
您的编译问题无关。您的其他答案解释了该问题的性质。
我的目录中有 3 个文件:
ex22.h、ex22.c。 ex22_main.c
ex22.h:
#ifndef _ex22_h
#define _ex22_h
extern double* v;
...
#endif
ex22.c:
#include <stdio.h>
#include "ex22.h"
double* v;
/*a function that accepts a new_variable, changes a
static variable inside of it while keeping track of
what the static variable was before the change. The
purpose of the function is to update the static
variable and print what it was right before the
updating.*/
double update_variable(double new_variable)
{
static double variable = 1.0;
double old_variable = variable;
variable = new_variable;
v = &variable;
return old_variable;
}
...
ex22_main.c:
#include "ex22.h"
#include "dbg.h"
...
int main(void)
{
//test if it is possible
printf("Variable at first: %f", update_variable(2.0);
printf("Access 'variable' inside update_variable: %f", *v);
}
Compiller (ubuntu) 给我这些错误信息:
cc ex22_main.c -o ex22_main
/tmp/ccGLFiXP.o: In function `main':
...
ex22_main.c:(.text+0x1f4): undefined reference to `update_variable'
ex22_main.c:(.text+0x222): undefined reference to `r'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'ex22_main' failed
make: *** [ex22_main] Error 1
希望您理解我想要实现的目标。我的目标是通过指向它的指针来访问函数内部的静态变量(这是不可能的)。我只是想知道它是不是那样工作的?
EDIT:
我的代码中有一些愚蠢的错误,但通过指针访问函数内部静态变量的想法是完全可行的。
避免我的错误:
1) 确保文件已链接
2) 注意变量名
编译器,或者更准确地说是 linker,找不到 update_variable
或 r
的定义,因为你没有't 编译和 link ex22.c 其中包含这些定义。
您需要编译这两个文件并将它们 link 在一起。
cc -o ex22_main ex22_main.c ex22.c
My goal is to access the static variable inside a function (which is not possible) by making a pointer to it. I'm just curious if it works that way?
是的,确实如此。 static
对于在函数内部声明的变量与在文件范围内声明的变量具有不同的含义,但是这两种变体都存在并且在程序的整个生命周期内具有相同的地址。指向此类变量的指针可用于在程序中的任何位置访问其值,而不管该变量是否可以通过其在访问指针时声明的标识符进行访问。
您的编译问题无关。您的其他答案解释了该问题的性质。