By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

What is "typedef struct" ?

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.