Connecting Tech Pros Worldwide Forums | Help | Site Map

question on structures.

broli
Guest
 
Posts: n/a
#1: Mar 26 '08

typedef struct y
{
x1 *c;
x2 *c;
} y;

typedef struct x1
{
y *a;
}x1;

typedef struct x2
{
y *b;
}x2;


^^ If I want to have declarations like that in my program, then what
should I do ? Would it work if I put these two lines before the
declaration of y -

typedef struct x1 x1;
typedef struct x2 x2;

Peter Nilsson
Guest
 
Posts: n/a
#2: Mar 26 '08

re: question on structures.


broli <Brol...@gmail.comwrote:
Quote:
typedef struct y
{
* x1 *c;
* x2 *c;
} y;
>
typedef struct x1
{
* y *a;
}x1;
>
typedef struct x2
{
* y *b;
>
}x2;
>
^^ If I want to have declarations like that in my
program, then what should I do ?
Read the FAQ.

http://www.c-faq.com/decl/mutrefstructs.html
Quote:
Would it work if I put these two lines before the
declaration of y -
>
typedef struct x1 x1;
typedef struct x2 x2;
Yes, but you don't need the typedefs...

struct y
{
struct x1 *c;
struct x2 *c;
};

struct x1
{
struct y *a;
};

struct x2
{
struct y *b;
};

--
Peter
broli
Guest
 
Posts: n/a
#3: Mar 26 '08

re: question on structures.


On Mar 26, 10:56 am, Peter Nilsson <ai...@acay.com.auwrote:
Quote:
broli <Brol...@gmail.comwrote:
Quote:
typedef struct y
{
x1 *c;
x2 *c;
} y;
>
Quote:
typedef struct x1
{
y *a;
}x1;
>
Quote:
typedef struct x2
{
y *b;
>
Quote:
}x2;
>
Quote:
^^ If I want to have declarations like that in my
program, then what should I do ?
>
Read the FAQ.
>
http://www.c-faq.com/decl/mutrefstructs.html
>
Quote:
Would it work if I put these two lines before the
declaration of y -
>
Quote:
typedef struct x1 x1;
typedef struct x2 x2;
>
Yes, but you don't need the typedefs...
>
struct y
{
struct x1 *c;
struct x2 *c;
};
>
struct x1
{
struct y *a;
};
>
struct x2
{
struct y *b;
};
>
Why is it wrong to use typedef here ? Or are you trying to say that if
I precede the struct x1 and x2 declarations with following two
statements -
typedef struct x1 x1;
typedef struct x2 x2;

Then I don't need to add the typedef keyword again when declaring the
structs ?
broli
Guest
 
Posts: n/a
#4: Mar 26 '08

re: question on structures.


Specifically, this is my problem -

typedef struct VertexData VertexData;
typedef struct EdgeData EdgeData;
typedef struct TriangleData TriangleData;

typedef struct HalfData{
HalfData *next;
HalfData *previous;
HalfData *next;
VertexData *origin;
TriangleData *left;
EdgeData *edge;
}HalfData;

struct VertexData{
HalfData* half;
};

struct EdgeData{
HalfData* half;
};

struct PolygonData{
HalfData* half;
};

Have I used a wrong notation over here ?
Nick Keighley
Guest
 
Posts: n/a
#5: Mar 26 '08

re: question on structures.


On 26 Mar, 06:09, broli <Brol...@gmail.comwrote:
Quote:
On Mar 26, 10:56 am, Peter Nilsson <ai...@acay.com.auwrote:
Quote:
broli <Brol...@gmail.comwrote:
Quote:
Quote:
Quote:
typedef struct y
{
* x1 *c;
* x2 *c;
} y;
>
Quote:
Quote:
typedef struct x1
{
* y *a;
}x1;
>
Quote:
Quote:
typedef struct x2
{
* y *b;
>
Quote:
Quote:
}x2;
>
Quote:
Quote:
^^ If I want to have declarations like that in my
program, then what should I do ?
>
Quote:
Read the FAQ.
>>
Quote:
Quote:
Would it work if I put these two lines before the
declaration of y -
>
Quote:
Quote:
typedef struct x1 x1;
typedef struct x2 x2;
>
Quote:
Yes, but you don't need the typedefs...
>
Quote:
* struct y
* {
* * struct x1 *c;
* * struct x2 *c;
* };
>
Quote:
* struct x1
* {
* * struct y *a;
* };
>
Quote:
* struct x2
* {
* * struct y *b;
* };
>
Why is it wrong to use typedef here ?
he's not saying the typedefs are "wrong" he's saying
they are unnecessary. Many C programmers don't like
typedefing structs. Many do. It's a style thing.
(I'm a typedefer myself, I thing making struct
part of the type name is ugly).

Quote:
Or are you trying to say that if
I precede the struct x1 and x2 declarations with following two
statements -
typedef struct x1 x1;
typedef struct x2 x2;
>
Then I don't need to add the typedef keyword again when declaring the
structs?
you can declare structs without using typedef.


--
Nick Keighley
santosh
Guest
 
Posts: n/a
#6: Mar 26 '08

re: question on structures.


broli wrote:
Quote:
Specifically, this is my problem -
>
typedef struct VertexData VertexData;
typedef struct EdgeData EdgeData;
typedef struct TriangleData TriangleData;
>
typedef struct HalfData{
HalfData *next;
HalfData *previous;
HalfData *next;
This is a duplicate member.
Quote:
VertexData *origin;
TriangleData *left;
EdgeData *edge;
}HalfData;
I would define the above as a plain struct declaration and later typedef
it.

struct HalfData {
/* ... */
};

typedef struct HalfData HalfData;

Also I don't like the practise of giving same names for both the struct
tags and the corresponding typedef, but that is a style issue.
Quote:
struct VertexData{
HalfData* half;
};
>
struct EdgeData{
HalfData* half;
};
>
struct PolygonData{
HalfData* half;
};
>
Have I used a wrong notation over here ?
Yes, for HalfData declaration.

broli
Guest
 
Posts: n/a
#7: Mar 26 '08

re: question on structures.


On Mar 26, 2:48 pm, santosh <santosh....@gmail.comwrote:
Quote:
broli wrote:
Quote:
Specifically, this is my problem -
>
Quote:
typedef struct VertexData VertexData;
typedef struct EdgeData EdgeData;
typedef struct TriangleData TriangleData;
>
Quote:
typedef struct HalfData{
HalfData *next;
HalfData *previous;
HalfData *next;
>
This is a duplicate member.
>
Quote:
VertexData *origin;
TriangleData *left;
EdgeData *edge;
}HalfData;
>
I would define the above as a plain struct declaration and later typedef
it.
>
struct HalfData {
/* ... */
};
>
typedef struct HalfData HalfData;
>
Also I don't like the practise of giving same names for both the struct
tags and the corresponding typedef, but that is a style issue.
>
Quote:
struct VertexData{
HalfData* half;
};
>
Quote:
struct EdgeData{
HalfData* half;
};
>
Quote:
struct PolygonData{
HalfData* half;
};
>
Quote:
Have I used a wrong notation over here ?
>
Yes, for HalfData declaration.
Ok so I take your suggestions -

typedef struct VertexDataStruct VertexData;
typedef struct EdgeDataStruct EdgeData;
typedef struct TriangleDataStruct TriangleData;

struct HalfDataStruct{
struct HalfDataStruct *next;
struct HalfDataStruct *previous;
struct HalfDataStruct *next;
};

typedef struct HalfDataStruct HalfData;

( I still don't see how this is different from what i did previously
ie typedef struct HalfData{
......}Halfdata;)

struct VertexDataStruct{
....
};

struct EdgeDataStruct{
....
};

struct TriangleDataStruct{
....
};
broli
Guest
 
Posts: n/a
#8: Mar 26 '08

re: question on structures.


On Mar 26, 3:47 pm, broli <Brol...@gmail.comwrote:
Quote:
On Mar 26, 2:48 pm, santosh <santosh....@gmail.comwrote:
>
>
>
Quote:
broli wrote:
Quote:
Specifically, this is my problem -
>
Quote:
Quote:
typedef struct VertexData VertexData;
typedef struct EdgeData EdgeData;
typedef struct TriangleData TriangleData;
>
Quote:
Quote:
typedef struct HalfData{
HalfData *next;
HalfData *previous;
HalfData *next;
>
Quote:
This is a duplicate member.
>
Quote:
Quote:
VertexData *origin;
TriangleData *left;
EdgeData *edge;
}HalfData;
>
Quote:
I would define the above as a plain struct declaration and later typedef
it.
>
Quote:
struct HalfData {
/* ... */
};
>
Quote:
typedef struct HalfData HalfData;
>
Quote:
Also I don't like the practise of giving same names for both the struct
tags and the corresponding typedef, but that is a style issue.
>
Quote:
Quote:
struct VertexData{
HalfData* half;
};
>
Quote:
Quote:
struct EdgeData{
HalfData* half;
};
>
Quote:
Quote:
struct PolygonData{
HalfData* half;
};
>
Quote:
Quote:
Have I used a wrong notation over here ?
>
Quote:
Yes, for HalfData declaration.
>
Ok so I take your suggestions -
>
typedef struct VertexDataStruct VertexData;
typedef struct EdgeDataStruct EdgeData;
typedef struct TriangleDataStruct TriangleData;
>
struct HalfDataStruct{
struct HalfDataStruct *next;
struct HalfDataStruct *previous;
struct HalfDataStruct *next;
};
>
typedef struct HalfDataStruct HalfData;
>
( I still don't see how this is different from what i did previously
ie typedef struct HalfData{
.....}Halfdata;)
>
struct VertexDataStruct{
...
>
};
>
struct EdgeDataStruct{
...
>
};
>
struct TriangleDataStruct{
...
>
};

^^ oops im sorry there i repeated the same mistake (duplicate member)
in half data but please ignore it as it is not signifcant to the
discussion
broli
Guest
 
Posts: n/a
#9: Mar 26 '08

re: question on structures.


I believe this is even a clearer approach as given in C FAQ 1.15 -

struct VertexDataStruct;
struct EdgeDataStruct;
struct TriangleDataStruct;

struct HalfDataStruct{
struct HalfDataStruct *next;
struct HalfDataStruct *previous;
struct VertexDataStruct *origin;
struct TriangleDataStruct *left;
struct EdgeDataStruct *edge;
};

struct VertexDataStruct{
struct HalfDataStruct *half;
};

struct EdgeDataStruct{
struct HalfDataStruct *half;
};

struct TriangleDataStruct{
struct HalfDataStruct *half;
};

typedef struct HalfDataStruct HalfData;
typedef struct VertexDataStruct VertexData;
typedef struct EdgeDataStruct EdgeData;
typedef struct TriangleDataStruct TriangleData;
Closed Thread