473,594 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is "typedef struct" ?

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 familiar with "typedef int NUMBER", but how does it work with
structures.

Tilak

Mar 21 '07 #1
8 40459
cman said:
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 familiar with "typedef int NUMBER", but how does it work with
structures.
First, remove the word "typedef" completely:

int NUMBER;

What would this do? It would define an int object named NUMBER.

Adding the typedef means "don't create an object - create a type synonym
instead".

struct { unsigned long pte_low; } pte_t;

What would this do? It would define a struct type with no tag, and would
also define an instance of that type, an object named pte_t.

Adding the typedef means "don't create an object - create a type synonym
instead".

It is perhaps easier to understand what's going on if you separate the
steps of defining the type and defining the synonym, but to do this you
will need to use a tag:

struct foo { unsigned long pte_low; };

int i; /* i is an object of type t */
struct foo bar; /* bar is an object of type struct foo */

typedef int i_t; /* i_t is a synonym for type int */
typedef struct foo bar_t; /* bar_t is a synonym for type struct foo */

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 21 '07 #2
Then how do you create more objects of a typedef struct with no tag?

Tilak
On Mar 21, 2:16 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
cman said:
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 familiar with "typedef int NUMBER", but how does it work with
structures.

First, remove the word "typedef" completely:

int NUMBER;

What would this do? It would define an int object named NUMBER.

Adding the typedef means "don't create an object - create a type synonym
instead".

struct { unsigned long pte_low; } pte_t;

What would this do? It would define a struct type with no tag, and would
also define an instance of that type, an object named pte_t.

Adding the typedef means "don't create an object - create a type synonym
instead".

It is perhaps easier to understand what's going on if you separate the
steps of defining the type and defining the synonym, but to do this you
will need to use a tag:

struct foo { unsigned long pte_low; };

int i; /* i is an object of type t */
struct foo bar; /* bar is an object of type struct foo */

typedef int i_t; /* i_t is a synonym for type int */
typedef struct foo bar_t; /* bar_t is a synonym for type struct foo */

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.

Mar 22 '07 #3
cman said:
Then how do you create more objects of a typedef struct with no tag?
There is no such thing as a "typedef struct", any more than there is any
such thing as an "= 6". The fact that the two symbols can legally
appear adjacently is irrelevant.

If you combine your structure definition with your typedef, then you
create a synonym for that type, which you can use thereafter, even if
the struct definition had no tag.

typedef struct { int x; int y; } point;

point p = { 0, 0 };
point q = { 1, 2 };

etc.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 22 '07 #4
At about the time of 3/21/2007 2:08 AM, cman stated the following:
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 familiar with "typedef int NUMBER", but how does it work with
structures.

Tilak
This allows you to define a structure type of pte_t, pgd_t, or pgprot_t
which you can use in your code. Ex:

struct pte_t { unsigned long pte_low; };

This defines the struct pte_t to the compiler, so when you need to
declare a variable to it:

struct pte_t varname;

Where as if you do:

typedef struct { unsigned long pte_low; } pte_t;

Then you can do:

pte_t varname;

with no struct tag because the compiler knows that pte_t is a structure
type when it encountered the typedef. Some people here will advise you
against doing this, and I forgot what the reason was. Here's some
real-world code that uses that format:
/* **** public types */

/* data transfer structure */
/* **note: if using a block algorithm such as AES, then
the output buffer must be at least as big as the input
buffer, and should be a multiple of the blocksize. */
typedef struct crypto_data_tag
{
uint32 sizein; /* datablock buffer size for input */
uint32 sizeout; /* datablock buffer size for output */
uint8 *datain; /* pointer to input datablock buffer */
uint8 *dataout; /* pointer to output datablock buffer */
} crypto_data_t;

/* crypto key data */
/* sub-structure used by other structures */
typedef struct crypto_key_tag
{
uint8 key[CRYPTO_KEYSIZE]; /* master crypto key */
uint8 salt[CRYPTO_SALTSIZE]; /* salt */
uint8 rand[CRYPTO_RANDSIZE]; /* random generator seed */
} crypto_key_t;

/* info transfer structure */
/* fully datafill this structure when initializing a crypto state */
typedef struct crypto_keydata_ tag
{
int algorithm; /* crypto algorithm to use */
int keysize; /* key size constant */
uint32 nonce; /* nonce counter */
uint32 skip; /* nonce counter skip */
crypto_key_t key; /* key information data */
} crypto_keydata_ t;
I think that some of the regulars hate me now after posting this...but
FWIW, I see ALOT of real-world code that does things this way. The
*ONE* problem that I have ever ran into doing it this way is linked
lists. You get stuff like this below:

typedef struct data_tag
{
void *data;
unsigned long size;
struct data_tag *next;
struct data_tag *prev;
} data_t;

Because it's not a complete type, you *MUST* use the struct identifier
with your initial tag as you would with a normal structure as shown
below. The only reason to have the initial structure name like _tag is
to solve linked list issues when doing it this way. Other than that,
you don't need it.
struct data_t
{
void *data;
unsigned long size;
struct data_t *next;
struct data_t *prev;
};

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Mar 22 '07 #5
On Mar 22, 10:04 am, Daniel Rudy <spamt...@spamt his.netwrote:
Because it's not a complete type, you *MUST* use the struct identifier
with your initial tag as you would with a normal structure as shown
below. The only reason to have the initial structure name like _tag is
to solve linked list issues when doing it this way. Other than that,
you don't need it.
I believe that struct names are always put in a different compiler
name-table than typedefs, so you can do things like
typedef struct s {
/* stuff */
} s;
and then you have the freedom either to use s or struct s.

Mar 22 '07 #6
cman wrote:
Then how do you create more objects of a typedef struct with no tag?
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html >
Mar 22 '07 #7
At about the time of 3/22/2007 3:12 AM, Fr************@ googlemail.com
stated the following:
On Mar 22, 10:04 am, Daniel Rudy <spamt...@spamt his.netwrote:
>Because it's not a complete type, you *MUST* use the struct identifier
with your initial tag as you would with a normal structure as shown
below. The only reason to have the initial structure name like _tag is
to solve linked list issues when doing it this way. Other than that,
you don't need it.

I believe that struct names are always put in a different compiler
name-table than typedefs, so you can do things like
typedef struct s {
/* stuff */
} s;
and then you have the freedom either to use s or struct s.
Hmm...that's interesting enough to try. I want to make a correction to
my original post though. Typedef does *NOT* define a type, struct does.
What typedef does is create an alias for a type identifier. So doing

typedef struct data_tag
{
int data1;
int data2;
} data_t;

creates the data_tag datatype and the alias data_t at the same time.
Which is equivilent to doing this:

struct data_tag
{
int data1;
int data2;
};
typedef struct data_tag data_t;

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Mar 22 '07 #8
Richard Heathfield <r...@see.sig.i nvalidwrote:
cman said:
Then how do you create more objects of a typedef struct with
no tag?

There is no such thing as a "typedef struct", any more than
there is any such thing as an "= 6". The fact that the two
symbols can legally appear adjacently is irrelevant.
Indeed. It may interest the OP that the following is legal
and equivalent to the original form (now snipped)...

struct { unsigned long pte_low; } typedef pte_t;

--
Peter

Mar 22 '07 #9

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

Similar topics

1
18974
by: Joel Kullberg | last post by:
Hi! I have a question for u guys! I would like to use the c++ package ITK (www.itk.org) for internal handling och data and functions in a dataset3D class of mine! I also want to use a base class to be able to use all kinds of datasets (2D/3D/4D...) ----------------------------------------------------------- class dataset_base{ public: virtual void printDataInfo()=0;
10
3499
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern struct foobar; ^
1
3551
by: Alfonso Morra | last post by:
Hi, I have the ff data types : typedef enum { VAL_LONG , VAL_DOUBLE , VAL_STRING , VAL_DATASET }ValueTypeEnum ;
3
4298
by: Pawe³ | last post by:
Hi! I want to read with C# some binary file with well defined binary structure. In C++ I was declaring appropriate struct, like: typedef struct {BYTE b1; WORD w1, w2; DWORD dw1} REKORD1; REKORD1 buff ; and then read record from file with read (file, (void *) &buff, sizeof (REKORD1));
0
1647
by: Gary | last post by:
Hi, there, I am going to use a command line parser class, which works well in VC6.0, in VC++7.1. However error message such as "C2059: syntax error :')'", and "C2061: syntax error : identifier 'CCmdLineParser_String'", and "C2061: syntax error : identifier 'LPCTSTR'" shows in compiling. What is the problem? Following is the header of the class: #if _MSC_VER > 1000 #pragma once
4
2476
by: Sacha | last post by:
I'm aware, that up to date, "typedef templates" are not defined within the C++ standard. The seemingly common workaround is this: template <class T> struct MyTypeDef { /* ultimately I need something like this: MyOtherClass<T> Type; but let's keep it simple for the moment */
8
28445
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
11
5634
by: sofeng | last post by:
I'm not sure if "data hiding" is the correct term, but I'm trying to emulate this object-oriented technique. I know C++ probably provides much more than my example, but I'd just like some feedback to find out if I've done anything wrong. Also, I am working on this for an embedded environment, so if there are great inefficiencies with this code, I'd like to know that also. I realize there is overhead with using an "accessor" function. ...
4
171348
by: msukumarbabu | last post by:
Hi all, What will be difference between "typedef enum" and "enum". or difference between “typedef structure" and "structure" I am going through some code. in that some place they are using enum without typedef. In some places they are using typedef before enumeration definition. Kindly provide some reference, for why they are using typedef.
0
7874
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8241
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8227
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6646
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5738
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5402
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.