В моем каталоге 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);
}
Компилятор (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
Надеюсь, вы поняли, чего я пытаюсь достичь. Моя цель - получить доступ к переменной stati c внутри функции (что невозможно), указав на нее указатель. Мне просто любопытно, работает ли он таким образом?
EDIT:
В моем коде были некоторые глупые ошибки, но идея доступа к переменной stati c внутри функции по ее указателю вполне осуществима.
Избегайте моих ошибок:
1) Убедитесь, что файлы связаны
2) Следите за именами переменных