简体   繁体   中英

Embedded C: Can I pass members of a static struct to function in another file, IF a pointer to the member is passed by a fn in the same file

I have a struct like this:

typedef struct {

   uint8_t var_1;
   uint8_t var_2;

}TYPE_struct_variables;

static TYPE_struct_variables variables;

For argument sake, I want to pass one of the members in this struct down another layer towards the metal. For this example, this is a data struct for an external device and I want to pass the member ' variables->var_1 ' / ' variables.var_1 ' to the usart interface of a host micro-controller. HOWEVER, I still want to restrict access to this struct for all functions outside this file. Would it work to send a pointer to this member if that pointer was sent by a fn in the same file as the struct?

Or would the program crash upon realizing it's a pointer inside a 'restricted' space?

C by itself doesn't know anything about restricted space in RAM. If you hide a variable as static it only won't get external linkage. That means it won't have a symbol outside of its compilation unit that you can reference to but it will have a regular address in the RAM or ROM (if you architecture supports that and the variable is constant) and that address is accessible for everybody.

Or in short: You can pass pointers to static variables in the same way as you can pass pointer to other variables or locations in RAM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM