using struct pointer before declaration

S

Serve Laurijssen

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.
 
E

Eric

Serve Laurijssen said:
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.
 
E

Eric Sosman

Serve said:
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.
 
P

Pieter Droogendijk

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;
};
 
N

Nick Austin

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.
 
J

Jarno A Wuolijoki

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.
 
E

Emmanuel Delahaye

Serve Laurijssen said:
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;
 

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,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top