Structue within a structure 
Author Message
 Structue within a structure

My program consists of about 15 seperate files.  I have a structure
declared before main:

typedef struct form {int a;  int b; } form;
form *FORM1;

Then, in an include that the outside files use:

struct form {int a;  int b;};
extern form;   (it may be extern FORM1, I can't remember, but it works
:)  )

My current problem is that I want to add a struct to the struct:

typedef struct inside {char b[4]; int a;} inside;

which I define above the form structure definition.  I then alter form
to read:

typedef struct form {int a; int b; inside INSIDE1;} form;

and do the same in the include file.

This works in the first file, but in the include file, the compiler
complains that it has run into type inside, when it was expecting a
closing bracket.

Anyone have any ideas?

Sincerely,

Joseph
--



Mon, 28 Jul 2003 03:59:49 GMT  
 Structue within a structure

Quote:
> My program consists of about 15 seperate files.  I have a structure
> declared before main:

> typedef struct form {int a;  int b; } form;

It helps to give the struct and the type slightly different names so you
know which you are referring to, like:

typedef struct form {int a;  int b; } Form;

Quote:
> form *FORM1;

Usually names in ALL CAPITALS are used for #defined constants and macros.
Try:

Form *form1Ptr;

Quote:

> Then, in an include that the outside files use:

> struct form {int a;  int b;};
> extern form;   (it may be extern FORM1, I can't remember, but it works
> :)  )

> My current problem is that I want to add a struct to the struct:

> typedef struct inside {char b[4]; int a;} inside;

> which I define above the form structure definition.  I then alter form
> to read:

> typedef struct form {int a; int b; inside INSIDE1;} form;

> and do the same in the include file.

> This works in the first file, but in the include file, the compiler
> complains that it has run into type inside, when it was expecting a
> closing bracket.

....because when it's parsing the header, the compiler doesn't know where or
if you typedeffed the structs.

Quote:

> Anyone have any ideas?

Put the typedefs in a header. #include the header in all the source files
that refer to the type.

/\/\/\/*=Martin
--



Mon, 28 Jul 2003 09:43:32 GMT  
 Structue within a structure

[...a description of his data structures...]

Can you show us the actual "before and after" code in the header
file and the main file?  I'm confused as to what's actually going
on here.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
--



Mon, 28 Jul 2003 09:43:41 GMT  
 Structue within a structure
Joseph a crit dans le message ...

Quote:
>My program consists of about 15 seperate files.  I have a structure
>declared before main:

>typedef struct form {int a;  int b; } form;

This is a type declaration;

Quote:
>form *FORM1;

This is a definition.

Quote:
>Then, in an include that the outside files use:

>struct form {int a;  int b;};

This is a structure declaration.

Quote:
>extern form;   (it may be extern FORM1, I can't remember, but it works
>:)  )

This is a structure declaration.

Quote:

>My current problem is that I want to add a struct to the struct:

>typedef struct inside {char b[4]; int a;} inside;

>which I define above the form structure definition.  I then alter form
>to read:

>typedef struct form {int a; int b; inside INSIDE1;} form;

>and do the same in the include file.

Why do you have 2 declarations? Headers files are here to avoid that.

Quote:
>This works in the first file, but in the include file, the compiler
>complains that it has run into type inside, when it was expecting a
>closing bracket.

This is really unclear. What sould be done is to put the declarations in the
headers, and the definitions in the C files. Just define things before you
use them. Add a guard in the header.

/* header.h */
#ifndef HEADER_H
#define HEADER_H

typedef struct
{
   char b[4];
   int a;

Quote:
} inside_t;

typedef struct
{
   int a;
   int b;
   inside_t INSIDE1;

Quote:
} form_t;

#endif /* HEADER_H */

/* source.c */
#include "header.h"

int main (void)
{
form_t form;

   return 0;

Quote:
}

--
-hs-    Tabs out, spaces in.
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc
--



Mon, 28 Jul 2003 09:42:42 GMT  
 Structue within a structure
I don't much like your indenting style or naming conventions, but I'll
preserve them for explanation's sake...



Quote:
> My program consists of about 15 seperate files.  I have a structure
> declared before main:

> typedef struct form {int a;  int b; } form;
> form *FORM1;

> Then, in an include that the outside files use:

> struct form {int a;  int b;};
> extern form;   (it may be extern FORM1, I can't remember, but it works
> :)  )

It's probably

  extern form *FORM1;

Quote:

> My current problem is that I want to add a struct to the struct:

> typedef struct inside {char b[4]; int a;} inside;

> which I define above the form structure definition.  I then alter form
> to read:

> typedef struct form {int a; int b; inside INSIDE1;} form;

> and do the same in the include file.

Does "the same" include defining "inside"?  Correctly?  Before the
definition of "form"?

Quote:

> This works in the first file, but in the include file, the compiler
> complains that it has run into type inside, when it was expecting a
> closing bracket.

> Anyone have any ideas?

Sounds like the files using the #include file don't know what an
"inside" is.  Because you've only defined it in main.c?  or you've
defined it incorrectly in the include file?

Why not just define everything in one place,  i.e., the header file, and
have main use that?  E.g.,

--- include.h

typedef struct inside {char b[4]; int a;} inside;
typedef struct form {int a; int b; inside INSIDE1;} form;
extern form FORM1;

--- main.c
#include "include.h"
/* FORM1 needs to be defined somewhere.  How about here? */
form *FORM1;
/*etc.*/

---other.c
#include "include.h"
/* We can use form and FORM1 here.  Example: */
form *dmy(void){return FORM1;}
/*etc.*/

---
This is closer to the way it was meant to be used.

HTH,

Icarus
--



Mon, 28 Jul 2003 09:43:57 GMT  
 Structue within a structure
Thanks for responding, all.  Some of your suggestions were very good,
especially considering my garbled letter :)

The way I ended up fixing it, is within the header file, within the
outside structure, when I placed the inside structure within it, I
actually has to specify what the structure was:

struct outside{
    int a;
    int b;
    struct inside { int a; int b;}
    };

and that did it :)

Thanks again for your help !

Joseph
--



Wed, 30 Jul 2003 17:32:36 GMT  
 Structue within a structure

Quote:

> My program consists of about 15 seperate files.  I have a structure
> declared before main:

> typedef struct form {int a;  int b; } form;
> form *FORM1;

> Then, in an include that the outside files use:

> struct form {int a;  int b;};
> extern form;   (it may be extern FORM1, I can't remember, but it works
> :)  )

If it works, then you probably had:

extern form *FORM1;

I would strongly advise you to take out the definition of form in your
main source file, and do the typedefing in your #include file.  As it
is now, you are redefining form.

Quote:

> My current problem is that I want to add a struct to the struct:

> typedef struct inside {char b[4]; int a;} inside;

> which I define above the form structure definition.  I then alter form
> to read:

> typedef struct form {int a; int b; inside INSIDE1;} form;

> and do the same in the include file.

> This works in the first file, but in the include file, the compiler
> complains that it has run into type inside, when it was expecting a
> closing bracket.

The idea behind a header file is you don't /have/ to write it twice.
In fact, you shouldn't, because it's not allowed.

Make sure you defined, or at least declared, the type inside above
form in the header file (you said you did in the main file).

Micah
--



Tue, 05 Aug 2003 12:30:20 GMT  
 Structue within a structure

Quote:

> My program consists of about 15 seperate files.  I have a structure
> declared before main:

> typedef struct form {int a;  int b; } form;
> form *FORM1;

    This is a good example of why NOT to use typedefs with structs
especially when you use the same type_name as struct_tag.

    Leave out the typedef, and:

struct form { int a; int b; };
struct form* FORM1;

    This will make the rest of your code easier to understand, and
will neither confuse you nor your compiler.

--Jeff Turner
--



Sun, 10 Aug 2003 00:43:18 GMT  
 Structue within a structure

Quote:

>> typedef struct form {int a;  int b; } form;
>> form *FORM1;
>     This is a good example of why NOT to use typedefs with structs
> especially when you use the same type_name as struct_tag.

Really? How so? Could you please justify your verdict a bit?

Quote:
>     Leave out the typedef, and:
> struct form { int a; int b; };
> struct form* FORM1;
>     This will make the rest of your code easier to understand, and
> will neither confuse you nor your compiler.

Sorry, but I can't see how adding the not-really-needed 'struct'
keyword all over the place would make the code easier to
read. IMHO. it only adds visual clutter.  Its absence in otherwise
syntactically corret code cannot ever "confuse" a compiler ---
whatever that might mean.  Nor do I think it can confuse a human
reader who knows his or her share of C.
--

Even if all the snow were burnt, ashes would remain.
--



Sun, 10 Aug 2003 05:00:21 GMT  
 Structue within a structure

Quote:



> >> typedef struct form {int a;  int b; } form;
> >> form *FORM1;

> >     This is a good example of why NOT to use typedefs with structs
> > especially when you use the same type_name as struct_tag.

> Really? How so? Could you please justify your verdict a bit?

> >     Leave out the typedef, and:

> > struct form { int a; int b; };
> > struct form* FORM1;

> >     This will make the rest of your code easier to understand, and
> > will neither confuse you nor your compiler.

> Sorry, but I can't see how adding the not-really-needed 'struct'
> keyword all over the place would make the code easier to
> read. IMHO. it only adds visual clutter.  Its absence in otherwise
> syntactically corret code cannot ever "confuse" a compiler ---
> whatever that might mean.  Nor do I think it can confuse a human
> reader who knows his or her share of C.

I'd suggest you read "Expert C Programming:  Deep C Secrets" by Peter
van der Linden, SunSoft Press, 1994.  ISBN 0-13-177429-8.  Chapter 3.

--Jeff Turner

PS.  Leaving out the struct keyword will only serve to make maintenance
of the code more difficult, as the next programmer will be scrambling
through header files and cursing your name.
--



Fri, 22 Aug 2003 10:59:55 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Structue within a structure

2. structures within structures

3. Q about structures within structures

4. Initialising Structure Within Structure...How?!

5. Converting to GUID structue?

6. how to dump easy-to-read structures from within C programs

7. How to use alias within a structure?

8. help: unnamed unions within structures

9. pointers within an array of structures

10. Sorting a last names that are contained within a structure

11. Union within structures

12. Help in initializing function pointers within structures

 

 
Powered by phpBB® Forum Software