简体   繁体   中英

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

In the MPLAB XC8 Compiler User Guide , an example on page 162 (reproduced below) uses the extern keyword in conjunction with the @ specifier. Given that we are specifying the address ourselves, why is this needed? It's not going to be allocating any memory per se.

The only reason I can think of is maybe extern variables aren't zeroed at startup. But then, C variables generally contain garbage anyway until you explicitly assign to them. So...I dunno.

Maybe it has something to do with it being in a header file? To avoid multiple #include statements causing a "variable already declared" error of some sort?


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.

It makes no difference. It's mainly a choice of being explicit vs. being implicit.

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