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

Why do typedef and struct go together?

Hi - I just learned C about two months ago, and I don't get why typedef and struct go hand in hand ! Can someone please explain?

Expand|Select|Wrap|Line Numbers
  1. typedef struct t_struct{
  2. int a;
  3. char t;
  4. };
  5.  
thanks...
Sep 8 '08 #1
3 1832
donbock
2,426 Expert 2GB
Hi - I just learned C about two months ago, and I don't get why typedef and struct go hand in hand ! Can someone please explain?

Expand|Select|Wrap|Line Numbers
  1. typedef struct t_struct{
  2. int a;
  3. char t;
  4. };
  5.  
You are not required to include "typedef" in structure declarations; but you're right, it is a pretty common idiom. The only incontrovertible advantage I can think of in Standard C for using a typedef name rather than a structure name is that you only have to type one word instead of two. There are some static analysis tools that will check that you used typedef names consistently, but that's beyond the scope of Standard C.

In Standard C, a typedef name is just a synonym for the type it is assigned to. You are allowed to vacillate back and forth between the two names. The only place I can think where a typedef name is preferred is when declaring a function that returns a function pointer: it is a whole lot easier to do this if you have a typedef for the returned function pointer.

Your mileage with C++ may vary. I don't know if that language tightened up typedef usage.

By the way, there is an error in your code snippet. It should be the following (assuming that you meant t_struct to be the name of the typedef). Notice the two entirely equivalent ways to declare variables of this type.
Expand|Select|Wrap|Line Numbers
  1. typedef struct S {
  2.     int a;
  3.     char t;
  4. } t_struct;
  5.  
  6. t_struct v1;
  7. struct S v2;
Cheers,
donbock
Sep 8 '08 #2
gpraghuram
1,275 Expert 1GB
typedef is very common when declaring function pointers also.
But as specified by the other member it is mostly used for structures to avoid the syntax of specifying struct every time you declare a variable.

raghu
Sep 9 '08 #3
JosAH
11,448 Expert 8TB
A bit of history may clarify the horrible typedef syntax: the earliest
C compilers (early 1970s) didn't have the typedef keyword. Repeating
the often complicating struct definitions or function prototypes all
over the place was a mess so they came up with a quick hack: the
typedef keyword acted as a storage class specifier, e. g.

Expand|Select|Wrap|Line Numbers
  1. static int t;
  2. ...
  3. typedef int t;
  4.  
Instead of defining an identifier t with a type int with a storage
class 'static' another name was defined which serves as an
alternative for the type int; (see the example). The hack caused
quite a complication for the lexical analyzer though because after
it scanned 't' it didn't know what to return to the parser: a type
or an identifier? (compare this after the lex-analyzer had seen
'int'; it would've known to tell the parse what it had just seen).

The scanner would simply return the token 'type-or-ident' and
leave the decision to the parser. The decision became a context
sensitive decision; see the next example:

Expand|Select|Wrap|Line Numbers
  1. typedef int t;
  2. t t(t t) { ... }
  3.  
Try to figure out what each 't' means in that text. Many wrong
implementations existed in those day until the Standards
attempted to straighten things out a bit. No matter what, the
typedef feature remains an ugly little hack.

kind regards,

Jos
Sep 9 '08 #4

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

Similar topics

4
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum {...
15
by: Merrill & Michele | last post by:
typedef struct { WORD versionNumber; WORD offset; } MENUITEMTEMPLATEHEADER; This is from vol 5 of unnamed platform's programmer's reference. I could make this conforming by enclosing...
2
by: Immo Birnbaum | last post by:
Hi, I'm trying to solve a programming lab assignment for my college C programming course, but as they taught us two semesters of Java before teaching us any C, I'm having problems with all the...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
6
by: Alex | last post by:
Hello people, I am getting errors from VS2003 when working with typedef'ed types. For example, assume that I have a type T, defined in a 3rd party include file based on some condition #if...
8
by: Ronny Mandal | last post by:
Consider these two: Typedef struct { int foo, bar } FooBar; struct FooBar { int foo, bar }; What woul be the only difference here; just that I can create new instances by 'Foobar fb, *pfb',...
15
by: Ian Bush | last post by:
Hi All, I'm a bit confused by the following which is causing one of our user's codes fail in compilation: typedef struct SctpDest_S; 1) Is this standard ? 2) If so ( or even if not so ! )...
17
by: Steve Carter | last post by:
Can someone please explain why the following is not possible? typedef foobar { int x; foobar *next; /* why can't we do this? */ }; Thanks
8
by: cman | last post by:
What does this kind of typedef accomplish? typedef struct { unsigned long pte_low; } pte_t; typedef struct { unsigned long pgd; } pgd_t; typedef struct { unsigned long pgprot; } pgprot_t I am...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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,...

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.