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

Why typedefs are not allowed to nest?

Hi All

I am new to this group.

Please explain the following program. I ran this program under Sun OS.

Here is the program.
Expand|Select|Wrap|Line Numbers
  1.         typedef struct emp
  2.         {
  3.  
  4.             typedef struct emp1
  5.             {
  6.                 int a;
  7.                 char c[10];
  8.                 float f;
  9.             } emp1;
  10.        };
  11.  
  12.        main()
  13.        {
  14.        }
  15.  
  16.  
Output:


"structsize1.c", line 4: syntax error before or at: typedef
"structsize1.c", line 4: cannot recover from previous errors
cc: acomp failed for structsize1.c


Why typedefs are not allowed to nest?


Thanks & Regards
Sathish
Sep 5 '07 #1
4 1581
sicarie
4,677 Expert Mod 4TB
Hi All

I am new to this group.

Please explain the following program. I ran this program under Sun OS.

Here is the program.
Expand|Select|Wrap|Line Numbers
  1.         typedef struct emp
  2.         {
  3.  
  4.             typedef struct emp1
  5.             {
  6.                 int a;
  7.                 char c[10];
  8.                 float f;
  9.             } emp1;
  10.        };
  11.  
  12.        main()
  13.        {
  14.        }
  15.  
  16.  
Output:


"structsize1.c", line 4: syntax error before or at: typedef
"structsize1.c", line 4: cannot recover from previous errors
cc: acomp failed for structsize1.c


Why typedefs are not allowed to nest?


Thanks & Regards
Sathish
I would start by fixing your main routine. Try:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.  
  4.    return 0;
  5. }
  6.  
It also won't hurt to put the standard include, depending on what language you are using.

::Edit:: while I believe you should do the things above anyway, I ran your code and it compiled and executed (you call nothing in main, so nothing is done), without errors on my G++ compiler in Cygwin. What compiler/version are you using? (gcc -v at command line)
Sep 5 '07 #2
Hi

Here is the modified version of the same code. In this I am trying to create the object of type of struct emp in main. Still it gives the same error for me.

I ran it under SunOS. I am using "cc" compiler.

Expand|Select|Wrap|Line Numbers
  1. typedef struct emp
  2. {
  3.  
  4.     typedef struct emp1
  5.     {
  6.         int a;
  7.         char c[10];
  8.         float f;
  9.     } emp1;
  10. }emp;
  11.  
  12. main()
  13. {
  14.   emp obj;
  15. }
  16.  

"structsize1.c", line 4: syntax error before or at: typedef
"structsize1.c", line 4: cannot recover from previous errors
cc: acomp failed for structsize1.c

I really dont know how it got worked for you without any errors.


Thanks & Regards
Sathish
Sep 5 '07 #3
sicarie
4,677 Expert Mod 4TB
Haha, yeah. I compiled it under G++, not GCC (GCC does give me those errors). I think it will depend on what version of C your CC compiler adheres to.
Sep 5 '07 #4
Banfa
9,065 Expert Mod 8TB
Why typedefs are not allowed to nest?
Because that is what the standard says. I wouldn't like to guess why but i shall anyway. I suspect when C was created (before the onset of OOP) it was not considered necessary so that is how the standard was created.

With the dawn of OOP and C++ it became apparent that objects may wish to have their own internal types so it was necessary to allow a typedef in a class to permit this, since a class and structure are virtually identical you also get typedefs in structures in C++.

In your example you can simply typedef the inner structure outside of the containing structure.

However you have another with is the structure emp contains no members which is also invalid in C.
Sep 5 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Hostile17 | last post by:
The question's all in the title really, but here's the context. I was writing a Perl script to fetch the CSS from pages and calculate the size of the images referenced in it. Then I remembered...
2
by: Levent | last post by:
Please consider the following Parent and Child template classes: template <class T> class Parent { public: typedef T type; typedef T& ref; /* pack of typedefs */ };
3
by: Steve Richter | last post by:
I have a .cs file that contains the c# script of a page: <script language="c#" runat=server src="item.cs"/> The file item.cs contains c# code that contains a namespace: namespace ItemPages {...
3
by: Josef K. | last post by:
Asp.net generates the following html when producing RadioButton lists: <td><input id="RadioButtonList_3" type="radio" name="MyRadioButtonList" value="644"...
1
by: Michal Wróbel | last post by:
Hello, I've got few questions concerning typedefs inside class definition. Firstly, I'll give an example code: template< class T > class Function { public: typedef boost::shared_ptr<...
30
by: junky_fellow | last post by:
I was looking at the source code of linux or open BSD. What I found that lots of typedefs were used. For example, consider the offset in a file. It was declared as off_t offset; and off_t is...
7
by: Noah Roberts | last post by:
I have something like the following: template<ENUM X, int I = 0> class Generic { .... }; typedef Generic<Val1> v1_type; typedef Generic<Val2> v2_type;
3
by: Steven Nagy | last post by:
Hi all, ASP.NET : Framework 2.0 - C# A recent addition to my code generater will create GridView's and ObjectDataSource's in a control (ASCX). So the code gen creates an ascx, ascx.cs,...
1
by: pauldepstein | last post by:
While reading some source code, I saw a variable called "end" of type "time". So I investigated what the type "time" meant and saw that time was a typedef for "Real". So what does "Real" mean? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.