“private” variable in c

  • A+
Category:Languages

Embedded Software where I can just use C.

I am reading the current revision of the circuit board out of an EEPROM.

Based on that revision-number the maximum supported current to the load can be defined.

Now I need that information on plenty of locations in the code (but read-only). Only the function that reads the EEPROM should be allowed to write the value. All other functions etc. must not change this value, an error could result in damaging the load or board.

Is there a way in c to do this? (NOT C++)

 


For a simple single-core embedded system, this is done by declaring the variable static at file scope in the EEPROM driver, such as:

static uint16_t max_current; 

The driver can then access this directly, but static prevents any other module from doing so. You give them read-only access by providing a getter function in the same driver:

uint16_t get_max_current (void) {   return max_current; } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: