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.