Order of definitions

  • Thread starter Mogens Heller Jensen
  • Start date
M

Mogens Heller Jensen

Hello group!

I have a (basic, I guess) question about the order of my definitions...

I have the following (a structure to wrap the data of a thread):

/* Thread data */
typedef struct tTkThread {
/* <snip>...</snip> */

void *pRunFunc;
} TkThread;

/* Thread procedure */
typedef void (*TkThreadProcedure)(TkThread *pThis);


My question is: How do I change the type of pRunFunc from void* to
TkThreadProcedure?

If I do my definitions in the order shown above, the compiler will complain
about not knowing the TkThreadProcedure function pointer type.

If I switch the order of the function pointer typedef and the structure def,
the compiler will complain that it does not know the TkThread structure.



Thanks in advance!

-Mogens
 
J

John Bode

Mogens said:
Hello group!

I have a (basic, I guess) question about the order of my definitions...

I have the following (a structure to wrap the data of a thread):

/* Thread data */
typedef struct tTkThread {
/* <snip>...</snip> */

void *pRunFunc;
} TkThread;

/* Thread procedure */
typedef void (*TkThreadProcedure)(TkThread *pThis);


My question is: How do I change the type of pRunFunc from void* to
TkThreadProcedure?

If I do my definitions in the order shown above, the compiler will complain
about not knowing the TkThreadProcedure function pointer type.

If I switch the order of the function pointer typedef and the structure def,
the compiler will complain that it does not know the TkThread structure.



Thanks in advance!

-Mogens

struct tTkThread;
typedef struct tTkThread TkThread;

typedef void (*TkThreadProcedure)(TkThread *pThis);

struct tTkThread {
...
TkThreadProcedure pRunFunc;
};
 
M

Mogens Heller Jensen

John Bode said:
struct tTkThread;
typedef struct tTkThread TkThread;

typedef void (*TkThreadProcedure)(TkThread *pThis);

struct tTkThread {
...
TkThreadProcedure pRunFunc;
};

Oh yeah, I tried something like that - but I see now that I forgot the
"struct" part of the typedef (I'm more used to C++ you see...).

I tried this in MSVC7 and it works perfectly. But it does not work in my
case - it seems the compiler is stupid/not fully compliant with the
standard. I am using the Codevision C compiler for the Atmel Atmega16
microcontroller.

Thanks for the input!

-Mogens
 
C

Chris Torek

[In the original, Mogens Heller Jensen shows code that does not compile
because the typedef-name occurs after the point where it is needed.]

I tried this in MSVC7 and it works perfectly.

It should; it is valid Standard C (both the old 1989 ANSI/ISO
standard, and the new 1999 ISO standard).
But it does not work in my case - it seems the compiler is stupid/not
fully compliant with the standard. I am using the Codevision C compiler
for the Atmel Atmega16 microcontroller.

You might try another newsgroup, as this one (comp.lang.c) is all
about Standard C, so we can only tell you that this compiler appears
to be broken (although to be sure of that, we would have to have a
complete sample that fails to compile).

On the other hand, you might also try removing all the "typedef"s
entirely. Remember that in C, typedef does not define types. It
just gives you a new name for an existing type. The above
can be rewritten as:

/* think of the keyword "struct" as meaning "type"; your type is
named TkThread */
struct TkThread {
...
void (*pRunFunc)(struct TkThread *pThis);
};

All you have to do is write out the "type" keyword each time,
spelling it "wrong": "STRange spelling for User-defined abstraCt
Type", or "struct".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top