1

So I have a list of clockings in my udata in, let's say, clock.c. I have read that udata has to be accessed through a pointer, well okay, fair enough.

Above defines.h

//Warning! This has to be conform the linkerscript, you can not just increase it.
#define CLOCKINGLISTLENGTH 50
#define KEYLENGTH 6

//A clocking consists out of a key, time and in/out (and status)
//All the clockings together are the "clockings" list.
//These clockings will await to be transmitted.
typedef struct
{
    unsigned isActive:1;
    unsigned needsTransmit:1;
    unsigned inOut:1;
    unsigned val1:1;
    unsigned val2:1;
    unsigned val3:1;
    unsigned val4:1;
    unsigned val5:1;
} byte;

typedef struct
{
    byte bits;
    unsigned char key[KEYLENGTH];
    unsigned char time[6];
} Clocking;

Above clock.c file.

#include "defines.h"
#pragma udata large_udata
Clocking clocking_queue[CLOCKINGLISTLENGTH];
#pragma udata
Clocking *clockings = clocking_queue;//Access udata through pointer!

In eeprom.c file.

#include "defines.h"
extern Clocking *clockings;

void writeClockingToEEPROM(char clockingIndex){
 int eepromIndex = 0;
 Write_b_eep(eepromIndex,clockings[clockingIndex].key[0]);
}

So my question is: "How to access udata from another file through a pointer". I simply want to use my clockings_queue (which should be accessed through *clockings pointer) in my EEPROM.c, so that I can store clockings in EEPROM.

Paul
  • 875
  • 7
  • 18

1 Answers1

0

The compiler doesn't know where clockings is in eeprom.c. There's nothing to tell the compiler that clockings is in clock.c.

In order to fix this, move extern Clocking *clocking; from eeprom.c into defines.h. This way, clock.c and eeprom.c know that clockings exists. The declaration of clockings should be in the header. The definition should be in clock.c (as it currently is).

Here's a link for an explanation on extern: http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/

CurtisHx
  • 995
  • 7
  • 12
  • Both include `defines.h`. Is it safe to forward-declare `Clocking *clocking` as an `extern` variable? Even when I declare it later on as a regular variable (in `clock.c`)? – Paul May 04 '15 at 14:52
  • @FauZe Make sure the contents of the header are wrapped with an `#ifndef [Some Name]` `#define [Some Name]` `#endif`. Or place a `#pragma once` statement at the top of the file. This will tell the compiler to use the file only once during compilation. I think the Microchip compilers support `#pragma once`. In order to use `clockings` in multiple c files, it needs to be forward declared in a header file. That way, the compiler knows about the variable. – CurtisHx May 04 '15 at 15:39