473,758 Members | 2,340 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 40470
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
18980
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
3516
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
3570
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
4304
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
1654
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
2481
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
28468
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
5654
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
171452
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
9492
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10076
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
9908
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
9885
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
9740
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
5175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3832
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3402
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2702
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.