Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > question on structures.

Reply
Thread Tools

question on structures.

 
 
broli
Guest
Posts: n/a
 
      03-26-2008

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;
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      03-26-2008
broli <(E-Mail Removed)> wrote:
> 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

> 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
 
Reply With Quote
 
 
 
 
broli
Guest
Posts: n/a
 
      03-26-2008
On Mar 26, 10:56 am, Peter Nilsson <(E-Mail Removed)> wrote:
> broli <(E-Mail Removed)> wrote:
> > 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
>
> > 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;
> };
>


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 ?
 
Reply With Quote
 
broli
Guest
Posts: n/a
 
      03-26-2008
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 ?
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      03-26-2008
On 26 Mar, 06:09, broli <(E-Mail Removed)> wrote:
> On Mar 26, 10:56 am, Peter Nilsson <(E-Mail Removed)> wrote:
> > broli <(E-Mail Removed)> wrote:


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

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

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


> 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
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-26-2008
broli wrote:

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

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

> struct VertexData{
> HalfData* half;
> };
>
> struct EdgeData{
> HalfData* half;
> };
>
> struct PolygonData{
> HalfData* half;
> };
>
> Have I used a wrong notation over here ?


Yes, for HalfData declaration.

 
Reply With Quote
 
broli
Guest
Posts: n/a
 
      03-26-2008
On Mar 26, 2:48 pm, santosh <(E-Mail Removed)> wrote:
> broli wrote:
> > 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.
>
> > 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.
>
> > struct VertexData{
> > HalfData* half;
> > };

>
> > struct EdgeData{
> > HalfData* half;
> > };

>
> > struct PolygonData{
> > HalfData* half;
> > };

>
> > 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{
....
};
 
Reply With Quote
 
broli
Guest
Posts: n/a
 
      03-26-2008
On Mar 26, 3:47 pm, broli <(E-Mail Removed)> wrote:
> On Mar 26, 2:48 pm, santosh <(E-Mail Removed)> wrote:
>
>
>
> > broli wrote:
> > > 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.

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

>
> > > struct VertexData{
> > > HalfData* half;
> > > };

>
> > > struct EdgeData{
> > > HalfData* half;
> > > };

>
> > > struct PolygonData{
> > > HalfData* half;
> > > };

>
> > > 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{
> ...
>
> };



^^ 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
 
Reply With Quote
 
broli
Guest
Posts: n/a
 
      03-26-2008
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;
 
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
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments