473,403 Members | 2,270 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,403 software developers and data experts.

struct declaration (silly question)

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 {
a_t *a;
} b_t;

int main(void) {

return 0;
}
</code>

my solution is to simply change type "b_t" to "void" but I'm not sure
if that's the best way of dealing with this issue.

Thanks,

--
Marcin Kasprzak
Mar 3 '08 #1
4 2066
Marcin Kasprzak <n...@email.addresswrote:
Hello Guys,
Silly question...
The question isn't; that you asked it is. It's a FAQ...

http://c-faq.com/decl/mutrefstructs.html

--
Peter
Mar 3 '08 #2
Marcin Kasprzak <no@email.addresswrites:
Silly question - what is the most elegant way of compiling a code similar
to this one?
[code for mutually referential structures]
Refer to the C FAQ.

1.14: I can't seem to define a linked list successfully. I tried

typedef struct {
char *item;
NODEPTR next;
} *NODEPTR;

but the compiler gave me error messages. Can't a structure in C
contain a pointer to itself?

A: Structures in C can certainly contain pointers to themselves;
the discussion and example in section 6.5 of K&R make this
clear. The problem with the NODEPTR example is that the typedef
has not been defined at the point where the "next" field is
declared. To fix this code, first give the structure a tag
("struct node"). Then, declare the "next" field as a simple
"struct node *", or disentangle the typedef declaration from the
structure definition, or both. One corrected version would be

struct node {
char *item;
struct node *next;
};

typedef struct node *NODEPTR;

and there are at least three other equivalently correct ways of
arranging it.

A similar problem, with a similar solution, can arise when
attempting to declare a pair of typedef'ed mutually referential
structures.

See also question 2.1.

References: K&R1 Sec. 6.5 p. 101; K&R2 Sec. 6.5 p. 139; ISO
Sec. 6.5.2, Sec. 6.5.2.3; H&S Sec. 5.6.1 pp. 132-3.
--
"I've been on the wagon now for more than a decade. Not a single goto
in all that time. I just don't need them any more. I don't even use
break or continue now, except on social occasions of course. And I
don't get carried away." --Richard Heathfield
Mar 3 '08 #3
On 3 Mar, 04:51, William Pursell <bill.purs...@gmail.comwrote:
On 3 Mar, 01:12, Marcin Kasprzak <n...@email.addresswrote:
Silly question - what is the most elegant way of compiling a code similar
to this one?
<snip code>

Others have mentioned the FAQ which deals with the necessary
forward declaration, but as a point of style, I recommend:
Ergg...no forward declaration necessary, of course. I
was distracted by the typedef's and '_t's and mistook
the question for a different FAQ.
Mar 3 '08 #4
Thanks guys for all your help,
now it works fine.

Once again thanks.

--
Marcin Kasprzak
Mar 3 '08 #5

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

Similar topics

7
by: Wellu Mäkinen | last post by:
Hi all, let's say I have a need to store 4 int values in somekind of a structure. I have atleast two options: class A { public: int a, b, c, b;
19
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at...
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
9
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
5
by: karthikbalaguru | last post by:
Hi, struct sky { int stars; struct ocean *oceanptr; }; struct ocean { int waves; struct sky *skyptr;
21
by: heavyz | last post by:
In C, if i want to declare a struct, there are 3 methods to do so: 1: struct dummy { ... }; 2: typedef struct { ... } dummy; 3: typedef struct _dummy { ... } dummy; Usually i use the first...
4
by: Ed | last post by:
Hi, guys, In C language manner, we need to put a "struct" token before one struct variable declaration like following. <code> struct Apple { float Price; };
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.