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

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 40433
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.invalidwrote:
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...@spamthis.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...@spamthis.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.invalidwrote:
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
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...
10
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...
1
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
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;...
0
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...
4
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...
8
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
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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,...
0
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...

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.