为什么在此 MPLAB C 示例中使用“extern”?

Why is `extern` used in this MPLAB C example?

MPLAB XC8 Compiler User Guide 中,第 162 页上的示例(转载如下)将 extern 关键字与 @ 说明符结合使用。鉴于我们自己指定地址,为什么需要这样做?它本身不会分配任何内存。

我能想到的唯一原因可能是外部变量在启动时没有归零。但是,C 变量通常包含垃圾,直到您显式分配给它们。所以...我不知道。

可能跟在头文件里有关系?要避免多个 #include 语句导致某种 "variable already declared" 错误?


If the pointer has to access objects in data memory, you need to define a different object to act as a dummy target. For example, if the checksum was to be calculated over 10 bytes starting at address 0x90 in data memory, the following code could be used.

const char * cp;
extern char inputData[10] @ 0x90;
cp = &inputData;
// cp is incremented over inputData and used to read values there

No memory is consumed by the extern declaration, and this can be mapped over the top of existing objects.

没有区别。主要是显式还是隐式的选择。