-1

I'm working on xinu, and I need to change some *.c files. I have this struct in file ready.c:

struct newtimer{
    struct newtimer* tnext;
    struct newtimer* tprev;
    int tkey;
    int tprio;
    int tcount;
};

and then I declared:

struct newtimer *timer = NULL;

I did some use with the timer variable in this file and I need to use it in another file as well (clkint.c). So in clkint I did this:

extern struct newtimer *timer;

(which compiles alright) but when i try to access timer's fields I get these errors: clkint errors

What am I doing wrong?

Thank you

edit:

As requested, here is some of the clkint.c:

struct newtimer *t;
extern struct newtimer *timer;
...
t = timer;
while(t!= NULL)
{
    ++(t->tcount);
    if(t->tcount >= 18){
        t->tcount = 0;
        newprior = proctab[t->tkey]->pprio + 10;
        t->tcount = newprior;
        chprio(t->tkey, newprior);
    }
    t = t->tnext;
    resched();
}

edit:

Replacing all the ts with timers does not solve the problem.

Alaa M.
  • 4,961
  • 10
  • 54
  • 95
  • Can we see the contents of the function clkint? – bstamour May 17 '13 at 21:39
  • 2
    It looks like you need a `.h` header file that declares your `newtimer` structure, so that you can insert it into clkint.c so that clkint.c becomes aware of the structure. – Robert Harvey May 17 '13 at 21:39
  • @RobertHarvey doesn't the *extern* keyword do the job? – Alaa M. May 17 '13 at 21:41
  • C compilers aren't as smart as, say, a Java compiler, or even a C++ compiler (which uses namespaces). It wouldn't surprise me if the header file was required. See the epic answer here: http://stackoverflow.com/a/1433387 – Robert Harvey May 17 '13 at 21:42
  • That's not really an accurate assessment with respect to how 'smart' a compiler is. C is a very precise, strong typed language. It makes little to no attempt to fill in the blanks for you, but the result of your code is very deterministic. The problem is more that an interface/API header needs to be created so a function is resolved across multiple source files. – Cloud May 17 '13 at 21:46
  • 1
    @Dogbert: `the result of your code is very deterministic` -- I never said it wasn't. But the OP is making the assumption that the C compiler can infer the structure by simply looking at the `extern` declaration, and I don't think it can. Nobody is insulting C compilers here. :) – Robert Harvey May 17 '13 at 21:52
  • 2
    Why would the definition of a C struct be exported when it is declared in a `.c` file? Unless you include that, how must the compiler know? – leppie May 17 '13 at 21:54
  • @leppie so you're saying the problem is in using the struct itself? but the compiler doesn't scream about `timer` (which is of type struct that's defined in the other file) but only about `timer`'s fields. – Alaa M. May 17 '13 at 22:16
  • @RobertHarvey Well put. My point is that C requires the developer to be very explicit when requesting access to resources. It would probably drive me nuts if my toolchain was trying to do this automatically, especially if there are conflicting definitions resolved at build time. – Cloud May 17 '13 at 22:18
  • 1
    Turbo C++ 3.00?!?! Really? – Carl Norum May 17 '13 at 22:23
  • @CarlNorum That's right ! – Alaa M. May 17 '13 at 22:29

1 Answers1

1

Your struct newtimer type is not defined. You probaly forgot to include the header file that defines struct newtimer.

When you use an unknown struct name in struct something, C compiler treats that as a forward declaration of a completely new struct type. The type is, of course, incomplete, which is why you are not allowed to access any innards of that type. The compiler simply knows nothing about those innards.

Where is your struct newtimer defined? If it is defined in a header file, you have to include it into your clkint.c.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • You, and all the others who said i need an `.h` file were right. I declared the struct in some `.h` file instead, included it in my files, and it worked like magic. – Alaa M. May 17 '13 at 22:28