简体   繁体   English

嵌入式C:我是否可以传递静态结构的成员以在另一个文件中运行,如果指向该成员的指针由同一文件中的fn传递

[英]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. 对于此示例,这是外部设备的数据结构,我想将成员' variables->var_1 '/' variables.var_1 '传递给主机微控制器的usart接口。 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? 如果该指针是由fn在与结构相同的文件中发送的,那么它是否可以发送指向该成员的指针?

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. C本身对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. 这意味着它不会在其编译单元之外有一个可以引用的符号,但它将在RAM或ROM中具有常规地址(如果您的架构支持并且变量是常量)并且该地址可供所有人访问。

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. 或者简而言之:您可以将指针传递给静态变量,方法与将指针传递给RAM中的其他变量或位置的方式相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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