0

So in my c file I have a variable:

static int32_t currentAddress = 0x00000000;

I was wondering if I use currentAddress to set fields within say struct A as 0. Then say I increment currentAddress elsewhere, will the field within A also change?

Basically I don't understand what static does in this case and when to use it.

5
  • 2
    in C static is just a scoping operator, the variable currentAddress is only visible within the current file. If the field within struct A is an int32_t than it will not change if you modify currentAddress. Commented Nov 20, 2013 at 20:07
  • It's better not to have static variables at all. Commented Nov 20, 2013 at 20:07
  • 1
    I'm sorry... why is it better not to have static variables? Perhaps you mean it is better not to have module level variables. But if you're going to have them, there are good reasons to prefer they be static. Commented Nov 20, 2013 at 20:08
  • Also, @MarkHendrickson, static used on a variable in a function would make the variable persist from call to call, that is, it wouldn't be created on the stack (in RAM) but in NVM. Commented Nov 20, 2013 at 20:08
  • @JeremyWest Yes, it's better not to have module level (global) variables, even static ones. static variable declared in a function is also not good - the function is not reentrant if you have a static variable in it. Commented Nov 20, 2013 at 20:10

2 Answers 2

2

The field within A will get the current value of currentAddress, which is 0. Changing currentAddress later won't affect the field of A unless you assign the field with currentAddress again, at which point the field of A will have the new value of currentAddress.

The static declarator sets the scope and lifetime of the variable currentAddress. You haven'r specified whether currentAddress is in file scope or inside a function. Either way, the variable will retain its value unless you modify it.

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

Comments

1

In case you mentioned, the valie is copied from currentAddress into the variable you are assigning to. So, changing currentAddress's value, will not change other values.

In C, static limits the variable's visibility to the current translation unit (in simpler terms, in the current source file, if the project has multiple source files). Also, it does not destroy variables at exiting from their scope, as it would happen with non-static variables. For example:

int foo(){
    int a = 0;
    a++;
    return a;
}

int bar(){
    static int a = 0;
    a++;
    return a;
}

Every call to foo() returns 1, because variable a is created, incremented, returned and destroyed. But, every call to bar() increases the value returned (it returns 1 first time, then 2, 3, 4, and so on), because the a variable is not destroyed anymore. Also note that the variable accessing rules are preserved: the a from bar cannot be accessed outside the bar function.

Comments

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.