View previous topic :: View next topic |
Author |
Message |
surfman19
Joined: 13 Dec 2007 Posts: 1
|
forward declaration |
Posted: Thu Dec 13, 2007 11:24 am |
|
|
I want to ask whether this c compiler support a forward declaration of a struct?
Code: |
struct foo;
struct test
{
struct foo a;
};
struct foo
{
int i;
};
|
|
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Thu Dec 13, 2007 3:55 pm |
|
|
Download the demo and try it out.
I would expect any compiler that requires explicit variables to throw an error. |
|
|
Guest
|
|
Posted: Fri Dec 14, 2007 2:40 pm |
|
|
its not working..but why?
cu |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Dec 14, 2007 3:07 pm |
|
|
Your code doesn't compile in MSVC++ 2005. It gives this error:
Quote: | error C2079: 'test::a' uses undefined struct 'foo' |
|
|
|
Guest
|
|
Posted: Fri Dec 14, 2007 4:51 pm |
|
|
Code: |
struct foo;
struct test
{
struct foo *a;
};
struct foo
{
int i;
};
|
this code is working with MSVC++ 2005 but not with ccs! why? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Dec 14, 2007 5:10 pm |
|
|
Don't know. Email CCS support and ask them. |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Sun Jul 06, 2008 11:54 pm |
|
|
Colleagues,
I want to have a structure that has a pointer to another structure of the same type.
Code: |
typedef struct
{
structS* pParent; // HSM parent state
}
structS;
|
Does the CCS compiler v4.071 support forward declaration? Is it still work in progress?
I've tried every example of the struct forward declaration I could find on the web (not shown in the code above), but none of them seemed to compile. I guess, I can have a void* pointer cast to structS* as a fallback approach.
I'll e-mail CCS support about this too.
Cheers,
- Nick _________________ Read the label, before opening a can of worms.
Last edited by kender on Mon Jul 07, 2008 12:05 pm; edited 1 time in total |
|
|
Ttelmah Guest
|
|
Posted: Mon Jul 07, 2008 4:34 am |
|
|
It is only latter languages like C++, that support forward declaration.
If you set VC, to compile using C syntax, it will error on this declaration.
It is actually very difficult to do, without dynamic memory management.
Just declare a pointer to a generic type, and reference this in the structure, then cast this to refer to the required type in use.
Best Wishes |
|
|
Ken Johnson
Joined: 23 Mar 2006 Posts: 197 Location: Lewisburg, WV
|
|
Posted: Mon Jul 07, 2008 10:00 am |
|
|
I think this is straight out of the old K&R reference (well, the struct def, anyway):
Code: |
#include "18F4520.h"
typedef struct sTag {
struct sTag *pS;
} structS;
void main (void)
{
structS s;
s.pS = 0;
for ( ;; ) ;
}
|
This comiles ok using V4.075
Good luck,
Ken |
|
|
Ttelmah Guest
|
|
Posted: Mon Jul 07, 2008 10:11 am |
|
|
Yes. That is not a forward reference, since you define the structure name before using it. Perfectly legitimate.
What C++ introduces is having an undeclared structure type referred to in a declaration.
What you post, should do what the original poster wants.
Best Wishes |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Mon Jul 07, 2008 11:40 pm |
|
|
Ken, thanks for the code! It worked for me. I've e-mail CCS support, and Darren had e-mailed me back his code, which was almost identical to yours.
I'm toying with an idea of OOP with ANSI C on the PIC. Here's a paper, which explains how to approach that www.cs.rit.edu/~ats/books/ooc.pdf.
I've tried to make my "class" one step more convoluted (notice the "self" pointer in the typedef for the function pointer):
Code: |
typedef void (*fptypeSomeMethod)(
structS* pSelf, // alas, structS is not yet declared at this point
int8 iSomeArg);
typedef struct _structS
{
fptypeSomeMethod fpMyMethod;
struct _structS* pSuper; // superclass
int8 m_iMyMemberVar;
}
structS;
|
But I think (goig back to Ttelmah's message) that can't be done without a pSelf being void*. _________________ Read the label, before opening a can of worms. |
|
|
Ttelmah Guest
|
|
Posted: Tue Jul 08, 2008 2:16 am |
|
|
Exactly.
It was this ability, that C++, added to the older syntax. You can have a void pointer, which automatically 'types itself', when used. However you can get the same basic 'effect', by just using a pointer to an existing type (like byte), and casting it when you use it. Just means you need to add the cast. Beware also, that you need to be very careful about the bracketing of the cast, if you perform pointer arithmetic.
Best Wishes |
|
|
|