0

I have 3 files in my directory:

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) gives me those error messages:

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

Hope you understood what I'm trying to achieve. 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?

EDIT:

There were some stupid bugs in my code but the idea of accessing a static variable inside a function by it's pointer is completely feasible.

Avoid my mistakes:

1) Make sure files are linked

2) Watch out for variable names

1
  • 1
    Yes you can do that of course. If this is good practice is another story. The error messages you get are explained by the answer below. Commented Apr 21, 2020 at 12:33

2 Answers 2

1

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?

Yes, it does. static has a different meaning for variables declared inside functions than for those declared at file scope, but but both varieties exist and have the same address for the entire lifetime of the program. A pointer to such a variable can be used to access its value anywhere in the program, without regard to whether the variable can be accessed via its declared identifier at the point of access-via-pointer.

Your compilation issue is unrelated. Your other answer explains the nature of that problem.

Sign up to request clarification or add additional context in comments.

Comments

0

The compiler, or more precisely the linker, can't find the definitions of update_variable or r because you didn't compile and link ex22.c which contains those definitions.

You need to compile both files and link them together.

cc -o ex22_main ex22_main.c ex22.c  

1 Comment

Quite so, but (oddly) that doesn't seem to be what the OP is actually asking about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.