C - 在多个函数中使用结构

Joh*_*ohn -2 c data-structures

我一直在检查谷歌一个小时.我尝试过使用typdef但是得到了相同的结果.我对结构范围有点困惑.我确定这只是一件我很遗憾的傻事.

示例,打印0:

#include <stdio.h>
struct info
{
    int i;
};
struct info testinfo;

int test()
{

    testinfo.i = 5;
}

int main()
{
    printf("%d", testinfo.i);
}
Run Code Online (Sandbox Code Playgroud)

Jen*_*ens 7

由于您将它们声明为局部变量,因此两个struct info都具有块范围.因此它们是不同的对象.在文件范围内(在任何函数之外)仅声明一个.

(有问题的代码已被编辑,此答案涉及最初的错误).