Question:
Can any one correct this nested structure? (c language)?
?
2011-07-13 11:44:22 UTC
include
main()
{
struct xx
{
int x;
struct yy
{
char s;
struct xx *p;

};
struct yy *q;
};
}

I am getting Compiler Error.
Three answers:
husoski
2011-07-13 13:55:58 UTC
Ignore the first part (at least!) of the first answer you got. There are some pretty decent programmers that answer questions around here in C++, C, Java, Javascript and VB, plus some folks that know Python, Assembler, Scheme, Perl, Ruby or other lesser-used tongues.



This compiled just fine with GCC, after I added a # to "#include". I'm surprised the C++ guru didn't catch that on the first bounce. It compiles with Visual C++ too, but turning on max warning levels points out two things that you want to change.



1. Make that "int main" or "int main(void)" instead of just "main()". The K&R style of function header has been deprecated for about 20 years now. A function needs a return type, and the return type for the main function must be int.



2. You define the layout of the (struct yy) type inside the definition of the (struct xx) type. That's not illegal, but it's more complicated than it needs to be. It's better to separate those as two different top-level declarations:



struct xx /* This is an xx object */

{

int x; /* An xx object contains an int */

struct yy *q; /* ...and points to a yy object */

};



struct yy /* This is a yy object */

{

char s; /* A yy object contains a char */

struct xx *p; /* ...and points to an xx object */

};



There's no advantage to having yy defined inside xx. The only time I'd recommend including a struct definition within another struct is when that inner struct occurs (as an object, not a pointer) inside the outer struct, and ONLY occurs there.



Edit: I just looked at my test program and added a few statement to test things out, and found out that the nested version exposes a bug in Visual C/C++ 2008 and 2010. At least, I think it's a bug. It treats the nested declaration of yy with xx as the declaration of an unnamed variable. As a result, the xx struct is bigger than it ought to be. GNU C works as expected, but it warns (reasonably) about declaration that doesn't declare anything.



This is yet another reason for separate declarations. Do things in a weird way, and you're more likely to run into less-tested areas of compiler behavior.
W.
2011-07-13 11:57:00 UTC
1. From now on, post questions like these in programming forums suck as Stack Overflow or www.daniwev.com



2. Try this



include

main()

{

struct xx

{

int x;

struct yy

{

char s;

xx *p; //you declared the struct twice

};

yy *q;//you declared the struct twice

};

}
?
2016-11-15 07:26:36 UTC
when I found out common in school (some years in the past) we had a fashion stated as "dry working", wherein we run a application on paper. It is going like that: technology a million: a=a million b=8 output="*" technology 2: a=a million b=7 output="**" technology 3: a=a million b=6 output="***" technology 4: a=a million b=5 output="****" technology 5: a=a million b=4 output="*****" technology 6: a=a million b=3 output="******" technology 7: a=a million b=2 output="*******n" technology 8: a=2 b=8 output="*" while entering into technology#7, b=2, in view that 2>a million the technology nonetheless occurs and the seventh megastar is outlined. Then - on the top(!) of the technology b-- takes b to be a million. In technology #8, a million>a million is fake and subsequently the (inner) technology does no longer ensue (and the execution is going to the outer technology). an incredible form of words, yet i'm hoping it clears out the difficulty.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...