473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

question on structures.


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;
Mar 26 '08 #1
8 1273
broli <Brol...@gmail.comwrote:
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
Mar 26 '08 #2
On Mar 26, 10:56 am, Peter Nilsson <ai...@acay.com.auwrote:
broli <Brol...@gmail.comwrote:
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 ?
Mar 26 '08 #3
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 ?
Mar 26 '08 #4
On 26 Mar, 06:09, broli <Brol...@gmail.comwrote:
On Mar 26, 10:56 am, Peter Nilsson <ai...@acay.com.auwrote:
broli <Brol...@gmail.comwrote:
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
Mar 26 '08 #5
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.

Mar 26 '08 #6
On Mar 26, 2:48 pm, santosh <santosh....@gmail.comwrote:
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{
....
};
Mar 26 '08 #7
On Mar 26, 3:47 pm, broli <Brol...@gmail.comwrote:
On Mar 26, 2:48 pm, santosh <santosh....@gmail.comwrote:
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
Mar 26 '08 #8
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;
Mar 26 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: kazack | last post by:
Hi all it's me again with another question as I got further in my book. The chapter I am in covers structres, abstract data and classes. I only read through to the end of the coverage on...
5
by: achrist | last post by:
Here's a program: #include <stdio.h> int main(int argc, char **argv) { double an_array = {0.0, 1.0, 2.0, 3.0, 4.0}; typedef struct a_struct {double v0, v1, v2,v3, v4;} a_type; typedef...
11
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to...
5
by: Shwetabh | last post by:
Hi everyone. My question is, why are data structures implemented only with struct data type? Why not union when it is more efficient as compared with structures? Thanks in advance
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
10
by: jack | last post by:
Hi guys, I am working on a project which requires an implementation of discrete event simulation in C using linked lists. I would greatly appreciate if someone could provide with some sources...
18
by: Steven | last post by:
Hi I'm new to js so this prob is a simple question but I haven't been able to solve it. I have 2 text fields which let me pick a graphic file and want to display then in 2 image place holders so...
15
by: sethukr | last post by:
Hi everybody, While running the following program in GCC, i'm very much screwed. main() { char *ptr1; char arr; int i; char *ptr2;
0
by: Art Cummings | last post by:
Good morning all. I just finished an assignment using structures. In this assignment I used an array of structures. What I would have liked was to use an array of structures with a function. ...
4
by: Marcin Kasprzak | last post by:
Hello Guys, Silly question - what is the most elegant way of compiling a code similar to this one? <code> typedef struct a { b_t *b; } a_t; typedef struct b {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.