Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   using struct pointer before declaration (http://www.velocityreviews.com/forums/t314403-using-struct-pointer-before-declaration.html)

Serve Laurijssen 07-25-2003 06:25 PM

using struct pointer before declaration
 
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.



Eric 07-25-2003 06:40 PM

Re: using struct pointer before declaration
 
Serve Laurijssen <ik@hier.nl> 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... ===

Eric Sosman 07-25-2003 06:51 PM

Re: using struct pointer before declaration
 
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.

--
Eric.Sosman@sun.com

Pieter Droogendijk 07-25-2003 08:13 PM

Re: using struct pointer before declaration
 
On Fri, 25 Jul 2003 20:25:49 +0200
"Serve Laurijssen" <ik@hier.nl> 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?-*s:(putchar(45),c
),putchar(main(c,k+=*s-c*-1,s+1)):(s=0);return!s?10:10+*k;}

Nick Austin 07-25-2003 08:14 PM

Re: using struct pointer before declaration
 
On Fri, 25 Jul 2003 20:25:49 +0200, "Serve Laurijssen" <ik@hier.nl>
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.


Arthur J. O'Dwyer 07-25-2003 09:12 PM

Re: using struct pointer before declaration
 

On Fri, 25 Jul 2003, Serve Laurijssen wrote:
>
> "Pieter Droogendijk" <gin@binky.homeunix.org> 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.

Jarno A Wuolijoki 07-25-2003 09:46 PM

Re: using struct pointer before declaration
 
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.


Emmanuel Delahaye 07-26-2003 06:45 PM

Re: using struct pointer before declaration
 
In 'comp.lang.c', "Serve Laurijssen" <ik@hier.nl> 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- emdelYOURBRA@noos.fr [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/


All times are GMT. The time now is 10:50 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.