CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

forward declaration

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
surfman19



Joined: 13 Dec 2007
Posts: 1

View user's profile Send private message

forward declaration
PostPosted: Thu Dec 13, 2007 11:24 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Dec 13, 2007 3:55 pm     Reply with quote

Download the demo and try it out.

I would expect any compiler that requires explicit variables to throw an error.
Guest








PostPosted: Fri Dec 14, 2007 2:40 pm     Reply with quote

its not working..but why?

cu
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 14, 2007 3:07 pm     Reply with quote

Your code doesn't compile in MSVC++ 2005. It gives this error:
Quote:
error C2079: 'test::a' uses undefined struct 'foo'
Guest








PostPosted: Fri Dec 14, 2007 4:51 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Dec 14, 2007 5:10 pm     Reply with quote

Don't know. Email CCS support and ask them.
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Sun Jul 06, 2008 11:54 pm     Reply with quote

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







PostPosted: Mon Jul 07, 2008 4:34 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jul 07, 2008 10:00 am     Reply with quote

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







PostPosted: Mon Jul 07, 2008 10:11 am     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Mon Jul 07, 2008 11:40 pm     Reply with quote

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







PostPosted: Tue Jul 08, 2008 2:16 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group