Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > using struct pointer before declaration

Reply
Thread Tools

using struct pointer before declaration

 
 
Serve Laurijssen
Guest
Posts: n/a
 
      07-25-2003
How can I get the following to compile?

typedef struct Y
{
void (*f)(X *x);
} Y;

typedef struct X
{
int x;
} X;

Just forward declaring X doesn't work and I can't put the word struct before
X, because the code is generated.


 
Reply With Quote
 
 
 
 
Eric
Guest
Posts: n/a
 
      07-25-2003
Serve Laurijssen <(E-Mail Removed)> wrote:

> typedef struct Y
> {
> void (*f)(X *x);
> } Y;
>
> typedef struct X
> {
> int x;
> } X;


Did you try:

typedef struct X
{
int x;
} X;

typedef struct Y
{
void (*f)(X *x);
} Y;

?

Famous last words: Works for me.


--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      07-25-2003
Serve Laurijssen wrote:
>
> How can I get the following to compile?
>
> typedef struct Y
> {
> void (*f)(X *x);
> } Y;
>
> typedef struct X
> {
> int x;
> } X;
>
> Just forward declaring X doesn't work and I can't put the word struct before
> X, because the code is generated.


The only way to get the code to compile as it stands
is to use a compiler for some non-C language.

An obvious way to fix it is to move the declaration of
`X' so it appears before the first use. But since you say
that another obvious fix isn't allowable, it's not clear
that this fix is applicable, either.

Fixing the program that generates the code is another
possibility.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Pieter Droogendijk
Guest
Posts: n/a
 
      07-25-2003
On Fri, 25 Jul 2003 20:25:49 +0200
"Serve Laurijssen" <(E-Mail Removed)> wrote:

> How can I get the following to compile?
>
> typedef struct Y
> {
> void (*f)(X *x);
> } Y;
>
> typedef struct X
> {
> int x;
> } X;


You probably mean a solution like this:

typedef struct Y Y;
typedef struct X X;

struct Y {
void (*f)(X *x);
};

struct X {
int x;
};

--
main(int c,char*k,char*s){c>0?main(0,"adceoX$_k6][^hn","-7\
0#05&'40$.6'+).3+1%30"),puts(""):*s?c=!c?-*sputchar(45),c
),putchar(main(c,k+=*s-c*-1,s+1))s=0);return!s?10:10+*k;}
 
Reply With Quote
 
Nick Austin
Guest
Posts: n/a
 
      07-25-2003
On Fri, 25 Jul 2003 20:25:49 +0200, "Serve Laurijssen" <(E-Mail Removed)>
wrote:

>How can I get the following to compile?
>
>typedef struct Y
>{
> void (*f)(X *x);
>} Y;
>
>typedef struct X
>{
> int x;
>} X;


You could just swap the order of both typedefs. Or:

struct X;
typedef struct X X;

typedef struct Y
{
void (*f)(X *x);
} Y;

struct X
{
int x;
};

Nick.

 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      07-25-2003

On Fri, 25 Jul 2003, Serve Laurijssen wrote:
>
> "Pieter Droogendijk" <(E-Mail Removed)> wrote in message
> >
> > typedef struct Y Y;
> > typedef struct X X;
> >
> > struct Y {
> > void (*f)(X *x);
> > };
> >
> > struct X {
> > int x;
> > };

>
> This certainly looks like it's working. I don't mean to be an ass, but
> it is correct C right?


Yes.
 
Reply With Quote
 
Jarno A Wuolijoki
Guest
Posts: n/a
 
      07-25-2003
On Fri, 25 Jul 2003, Eric Sosman wrote:

> > How can I get the following to compile?
> > ...

>
> The only way to get the code to compile as it stands
> is to use a compiler for some non-C language.
> Fixing the program that generates the code is another
> possibility.


Hmmm.. generalizing:

generatecode foo.almostc
fixcode foo.almostc foo.c
cc foo.c

The two first lines are the fixed generator and the last
two a non-C compiler.

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      07-26-2003
In 'comp.lang.c', "Serve Laurijssen" <(E-Mail Removed)> wrote:

> How can I get the following to compile?
>
> typedef struct Y
> {
> void (*f)(X *x);
> } Y;
>
> typedef struct X
> {
> int x;
> } X;
>
> Just forward declaring X doesn't work and I can't put the word struct
> before X, because the code is generated.


Sounds obvious, but wat's wrong with:

typedef struct
{
int x;
}
X;

typedef struct
{
void (*f)(X *x);
}
Y;

--
-ed- (E-Mail Removed) [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struct pointer vs. struct array pointer aleksa C Programming 16 02-20-2013 08:20 PM
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Initialize pointer-to-struct declaration to an unnamed struct? Ehud Shapira C++ 20 06-30-2007 04:10 PM
(: Pointer to struct withing pointer to struct :) Zero C Programming 16 11-19-2005 01:27 AM
passing pointer->struct->pointer->struct to function. .. ?? beetle C Programming 2 01-25-2005 06:08 PM



Advertisments