473,395 Members | 1,440 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.

incomplete struct in a struct

Hi,

struct sky {
int stars;
struct ocean *oceanptr;
};

struct ocean {
int waves;
struct sky *skyptr;
};

Will the compiler accept the field declaration struct ocean
*oceanptr within struct sky ?

Actually, the compiler has not yet heard of struct ocean till that
instant of time . ( struct ocean is ``incomplete'' at that point.) .

It has been stated that Just a empty declaration of the struct like
below will fix the issue -
struct sky;
struct ocean;

But, How is this possible ? What is happening here internally ? Is it
really like that ?

Thx in advans,
Karthik Balaguru

Aug 14 '07 #1
5 3601
karthikbalaguru wrote:
Hi,

struct sky {
int stars;
struct ocean *oceanptr;
};

struct ocean {
int waves;
struct sky *skyptr;
};

Will the compiler accept the field declaration struct ocean
*oceanptr within struct sky ?
This smells like homework.

Did you try this? What happened? What does your book/course notes say?
Actually, the compiler has not yet heard of struct ocean till that
instant of time . ( struct ocean is ``incomplete'' at that point.) .
Yes.
It has been stated that Just a empty declaration of the struct like
below will fix the issue -
What issue?
struct sky;
struct ocean;

But, How is this possible ?
Compiler writers (and language designers) are dead smart. (Sometimes,
it must be said, too smart for their own good, or possibly for their
users' good.)
What is happening here internally ?
The compiler notes that eg `oceanpointer` is a pointer to struct ocean,
and that it hasn't yet seen a definition for that struct. But it doesn't
need a definition of the struct to know about pointers to it.
Is it really like that ?
Is what really like what?

--
Chris "is this a rhetorical question?" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Aug 14 '07 #2
On Tue, 14 Aug 2007 05:39:05 -0700, karthikbalaguru
<ka***************@gmail.comwrote:
>Hi,

struct sky {
int stars;
struct ocean *oceanptr;
The standard requires that all pointers to struct, regardless of the
type of struct, have the same size, representation, alignment.
Therefore, even the compiler knows nothing about struct ocean, other
than that it is a struct of some kind, that is sufficient for the
compiler to properly place an object of type struct ocean* in struct
sky.

The real question is why didn't you just try it on your system. You
would have had your answer a lot faster than waiting for people on
Usenet to respond.
> };

struct ocean {
int waves;
struct sky *skyptr;
};

Will the compiler accept the field declaration struct ocean
*oceanptr within struct sky ?

Actually, the compiler has not yet heard of struct ocean till that
instant of time . ( struct ocean is ``incomplete'' at that point.) .

It has been stated that Just a empty declaration of the struct like
below will fix the issue -
struct sky;
struct ocean;

But, How is this possible ? What is happening here internally ? Is it
really like that ?

Thx in advans,
Karthik Balaguru

Remove del for email
Aug 18 '07 #3
Barry Schwarz wrote:
[...]
The real question is why didn't you just try it on your system. You
would have had your answer a lot faster than waiting for people on
Usenet to respond.
[...]

For the same reason you don't take your compiler's output of "i=i++"
to be definitive.

Besides, he also asked:
What is happening here internally ?
And this cannot be answered simply by compiling.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Aug 20 '07 #4
On Aug 14, 6:10 pm, Chris Dollin <chris.dol...@hp.comwrote:
karthikbalaguru wrote:
Hi,
struct sky {
int stars;
struct ocean *oceanptr;
};
struct ocean {
int waves;
struct sky *skyptr;
};
Will the compiler accept the field declaration struct ocean
*oceanptr within struct sky ?

This smells like homework.

Did you try this? What happened? What does your book/course notes say?
Actually, the compiler has not yet heard of struct ocean till that
instant of time . ( struct ocean is ``incomplete'' at that point.) .

Yes.
It has been stated that Just a empty declaration of the struct like
below will fix the issue -

What issue?
struct sky;
struct ocean;
But, How is this possible ?

Compiler writers (and language designers) are dead smart. (Sometimes,
it must be said, too smart for their own good, or possibly for their
users' good.)
What is happening here internally ?

The compiler notes that eg `oceanpointer` is a pointer to struct ocean,
and that it hasn't yet seen a definition for that struct. But it doesn't
need a definition of the struct to know about pointers to it.
Is it really like that ?

Is what really like what?

--
Chris "is this a rhetorical question?" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
Thx for the info.
That is called "Forward reference to structure ocean" .The reference
is preceded
by the struct keyword to resolve potential ambiguity. Using an
identifier before its declaration is called a forward reference, and
results in an error, except in few cases and the above 'structure
case' is one such case which does not throw any error.

I am sharing some interesting stuff collected from internet w.r.t
writing optimised code related to this forward reference as below :
Allowing Forward reference can greatly increase the complexity and
memory requirements of a compiler, and generally prevents the compiler
from being implemented in one pass(one pass compilation).

Thx,
Karthik Balaguru

Aug 27 '07 #5
dk
On Aug 27, 11:53 am, karthikbalaguru <karthikbalagur...@gmail.com>
wrote:
On Aug 14, 6:10 pm, Chris Dollin <chris.dol...@hp.comwrote:


karthikbalaguru wrote:
Hi,
struct sky {
int stars;
struct ocean *oceanptr;
};
struct ocean {
int waves;
struct sky *skyptr;
};
Will the compiler accept the field declaration struct ocean
*oceanptr within struct sky ?
This smells like homework.
Did you try this? What happened? What does your book/course notes say?
Actually, the compiler has not yet heard of struct ocean till that
instant of time . ( struct ocean is ``incomplete'' at that point.) .
Yes.
It has been stated that Just a empty declaration of the struct like
below will fix the issue -
What issue?
struct sky;
struct ocean;
But, How is this possible ?
Compiler writers (and language designers) are dead smart. (Sometimes,
it must be said, too smart for their own good, or possibly for their
users' good.)
What is happening here internally ?
The compiler notes that eg `oceanpointer` is a pointer to struct ocean,
and that it hasn't yet seen a definition for that struct. But it doesn't
need a definition of the struct to know about pointers to it.
Is it really like that ?
Is what really like what?
--
Chris "is this a rhetorical question?" Dollin
Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Thx for the info.
That is called "Forward reference to structure ocean" .The reference
is preceded
by the struct keyword to resolve potential ambiguity. Using an
identifier before its declaration is called a forward reference, and
results in an error, except in few cases and the above 'structure
case' is one such case which does not throw any error.

I am sharing some interesting stuff collected from internet w.r.t
writing optimised code related to this forward reference as below :
Allowing Forward reference can greatly increase the complexity and
memory requirements of a compiler, and generally prevents the compiler
from being implemented in one pass(one pass compilation).

Thx,
Karthik Balaguru- Hide quoted text -

- Show quoted text -
"except in few cases and the above 'structure
case' is one such case which does not throw any error."

Can you list out other such cases please?

-Dk

Aug 29 '07 #6

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

Similar topics

7
by: Andrew Ward | last post by:
Hi All, Considering the following code: struct A; struct B { std::list<A> l; };
5
by: Paul F. Dietz | last post by:
Is the following legal C? struct foo; struct foo (*p); /* Pointer to array of 10 foo structures */ struct foo { int bar; int baz; }; main() { printf("%d\n", sizeof(*p)); } Paul Dietz...
24
by: ark | last post by:
Hello group, Could you help me with this: static const int x; ............ something ............. static const int x = 17; It looks perfectly legal to me but MSVC/C++ 6.0 gives, on the...
6
by: Eric Smith | last post by:
Is a structure containing an incomplete array as its last element (per paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an incomplete type? That appears to be indicated by paragraph...
3
by: onsbomma | last post by:
Can anyone tell me the difference between typedef struct chunkinfo* mfastbinptr; and typedef struct chunkinfoptr chunkinfoptr;
2
by: aegis | last post by:
sizeof could not possibly evaluate to some size for a type, unless two things for a type occur: a) the type is complete and sizeof will evaluate to appropriate size b) type is not complete and...
2
by: Halid Umar A M | last post by:
Dear All, Please tell me why this error is occuring. The following is the code snippets which i have typed. struct mystructure{ struct list_head m; //error: field m has incomplete...
2
by: Kai-Uwe Bux | last post by:
Does the following have undefined behavior? struct X; struct Y { X * x_ptr; Y ( void ) : x_ptr ( 0 )
5
by: fmassei | last post by:
Hello, I'm have a question about forward structure declarations and incomplete types. The complete structure of my file is pretty complex, but just as an example the code below will explain the...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
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: 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
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
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.