473,396 Members | 1,724 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,396 software developers and data experts.

cascaded struct members.

Hi All

why does forward declaration does not work in following code.

struct A;
struct B
{
struct A a;

// struct A *a // WORKS SINCE ITS POINTER
};

struct A
{
struct B b;
};

gives compiler error for incomplete type A. why does forward
declaration of Struct A solve this problem.

Oct 16 '06 #1
6 1760
sunny wrote:
Hi All

why does forward declaration does not work in following code.

struct A;
struct B
{
struct A a;

// struct A *a // WORKS SINCE ITS POINTER
};

struct A
{
struct B b;
};

gives compiler error for incomplete type A. why does forward
declaration of Struct A solve this problem.
1. How big is A? The compiler doesn't know at the point you declare A a
in B.

2. How big is an A at the end of your code? If you were allowed to
have an A contain a full B, and vice versa, then it's infinitely sized:

A contains a B which contains an A which contains a B which ....

3. Works when a is an A* because the size of a pointer is well-defined.

Oct 16 '06 #2
sunny wrote:
why does forward declaration does not work in following code.

struct A;
struct B
{
struct A a;
Because the compiler NEEDS TO KNOW the internals of 'A' here.
>
// struct A *a // WORKS SINCE ITS POINTER
All pointers to objects are of the same size. The compiler does
NOT need to know the internals of 'A' here.
};

struct A
{
struct B b;
};

gives compiler error for incomplete type A. why does forward
declaration of Struct A solve this problem.
The last statement is false. A forward declaration of 'A' does
NOT solve the problem of mutual containment. Nothing can. If
each object 'B' contains a member of type 'A', and, in turn, each
object 'A' contains a member of type 'B', what's the size of the
objects?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 16 '06 #3
sunny wrote:
Hi All

why does forward declaration does not work in following code.

struct A;
struct B
{
struct A a;
You must have a complete definition here since you actually
allocate one.
>
// struct A *a // WORKS SINCE ITS POINTER
};

struct A
{
struct B b;
};

gives compiler error for incomplete type A. why does forward
declaration of Struct A solve this problem.
You have A containing a B which contains an A which contains a B ...

How big is this object?
Oct 16 '06 #4

Ron Natalie wrote in message
<45**********************@news.newshosting.com>. ..
>sunny wrote:
>Hi All

why does forward declaration does not work in following code.

struct A;
struct B{
struct A a;
};

struct A{
struct B b;
};

gives compiler error for incomplete type A. why does forward
declaration of Struct A solve this problem.

You have A containing a B which contains an A which contains a B ...

How big is this object?
Same size as the universe + 1.

I'm running the calculation on my computer now...... uhh....might be a while.
I'll get back to you on this when it's done...
<millions of years pass>
Ahhh, 1% done.... won't be long now...

--
Bob <GR
POVrookie
Oct 16 '06 #5
BobR wrote:
>>You have A containing a B which contains an A which contains a B ...

How big is this object?

Same size as the universe + 1.

I'm running the calculation on my computer now...... uhh....might be a
while. I'll get back to you on this when it's done...
<millions of years pass>
Ahhh, 1% done.... won't be long now...
Your computer is broken. Any decent computer after that long calculation
must tell that the answer is 42.

--
Salu2
Oct 16 '06 #6

Julián Albo wrote in message <45********@x-privat.org>...
BobR wrote:
>>>You have A containing a B which contains an A which contains a B ...
How big is this object?

Same size as the universe + 1.

I'm running the calculation on my computer now...... uhh....might be a
while. I'll get back to you on this when it's done...
<millions of years pass>
Ahhh, 1% done.... won't be long now...
>Your computer is broken. Any decent computer after that long calculation
must tell that the answer is 42.
I just checked with Duck Dogers (remember Daffy Duck?) in the year 2525, and
he said the output was:
42e+std::numeric_limits<long long>::max()
....as the calculation passed through his time period. So, obviously YOU
misplaced a decimal point!!
[ gads, you'd think with a 1024bit 7.2TeraHz CPU (low end machine) the 'long
long' would be deprecated by then!! ]

Yup, that duck really quacks me up!
--
Bob <GR
POVrookie
Oct 17 '06 #7

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

Similar topics

2
by: Michael B Allen | last post by:
My understanding is that struct members will remain in order such that the following can be used to support variable sized members: struct indexed_values { int *values; unsigned char bitset;...
12
by: Jalapeno | last post by:
I've a CICS program that uses BMS maps. Our wysiwyg map editor produces it's source in COBOL so I am manually creating C unions to match the COBOL output. Can I convert the FILLER statements to...
11
by: edson | last post by:
Greetings For certain operations I would like to have easy access to struct members. Here is an example. struct mystruct { char member1; char member2; char member3; };
18
by: lovecreatesbea... | last post by:
1. The following code snippet uses minus operation on two pointers to calculate the distance between struct members. This is illegal, right? 2. s1 and s2 are type of the same struct S. Can the...
4
by: MetaKith | last post by:
Hi, Please have a look on the next code : struct foo{ float b; char z; }; struct foo foo_init = {234.77,"xcdy"}; struct foo...
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
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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.