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

typedef help

P: n/a
Hi all,

Have alook at two different declarations below:

typedef unsigned char uc;

typedef void (*fptr) (void);

The first declaration has a left part which is a C keyword: "unsigned char"
and the declaration is clarly understandable.
But the second declaration has neither a keyword nor left-right parts.
Can anybody explain how compiler treats such a declaration below ? ( if we
have a function fun() )

const fptr fun;

Thanks & regards,
Shailendra
Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mehta Shailendrakumar wrote:
Hi all,

Have alook at two different declarations below:

typedef unsigned char uc;

typedef void (*fptr) (void);

The first declaration has a left part which is a C keyword: "unsigned char"
and the declaration is clarly understandable.
But the second declaration has neither a keyword nor left-right parts.
Not true. Both typedefs are easily read. You just have to read them just
like you would read any declaration.

Given
unsigned char uc;
you would read this as
"uc" is an unsigned char.

And
void (*fptr) (void);
you would read as
"fptr" is a pointer to a function accepting void, returning void

You read the typedefs in a similar manner. For instance
typedef unsigned char uc;
reads as
"uc" is an alias for an unsigned char

and
typedef void (*fptr) (void);
reads as
"fptr" is an alias for a pointer to a function accepting void,
returning void

Can anybody explain how compiler treats such a declaration below ? ( if we
have a function fun() )

const fptr fun;


Given the typedef above, then this reads as
"fun" is a const pointer to a function accepting void, returning void
- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFCsuO8agVFX4UWr64RAidKAJ914YJdrpa/0czva+Ktlp9UjfmLdQCfe8W6
1Cr9X6rWNi5G3ubh1Tl8GtI=
=wPU0
-----END PGP SIGNATURE-----
Nov 14 '05 #2

P: n/a

Mehta Shailendrakumar wrote:
Hi all,

Have alook at two different declarations below:

typedef unsigned char uc;

typedef void (*fptr) (void);

The first declaration has a left part which is a C keyword: "unsigned char"
and the declaration is clarly understandable.
But the second declaration has neither a keyword nor left-right parts.
Time for a brief refresher on C declaration syntax.

Remember that C follows a "declaration mimics use" paradigm, meaning
that an object declaration should look as much as possible like an
object reference in the code. For example, suppose I had an array of
pointers to int, and I wanted to get the int value pointed to by a
particular element. I would therefore use the expression

*a[i]

to get that int value.

So, when I declare the array, the declaration should look as much like
that expression as possible. Since the type of the expression *a[i] is
int, the declaration is

int *a[ARRAY_SIZE];

The expression "int" is the type specifier. The expression
"*a[ARRAY_SIZE]" is the *declarator*. The expression "a" is the
*identifier*.

The type specifier provides the basic type information for the
identifier. You could say it specifies the "int-ness" of a. The
declarator provides additional type information not given by the type
specifier. In this case, the declarator specifies the "array-ness" and
"pointer-ness" of a.

In addition to the type specifier and declarator, there is another type
of specifier called the storage class specifier, which indicates how
the item is to be stored. For example,

static int *a[ARRAY_SIZE];

inticates that the array should be stored in such a way that it
persists for the lifetime of the program.

The typedef keyword is a storage class specifier like static, although
its semantics are a bit different from the other storage class
specifiers. It specifies that the identifier in the declarator be used
as a synonym for a type. For example:

int a; // a is an object of type int
typedef int a; // the expression "a" is a synonym for type int
int *a; // a is an object of type pointer to int
typedef int *a; // the expression "a" is a synonym for type int *.

int *a[10]; // a is a 10-element array of pointers to int
typedef int *a[10]; // the expression "a" is a synonym for type int
*[10]

And, from your specific example above:

void (*fptr)(void); // fptr is an object of type pointer to
// function taking no parameters and
// returning void

typedef void (*fptr)(void); // fptr is a synonym for type pointer to
// function taking no parameters and
// returning void

Can anybody explain how compiler treats such a declaration below ? ( if we
have a function fun() )

const fptr fun;

This statement declares fun as a constant pointer to a function taking
no parameters and returning void. It is the equivalent of declaring

const void (*fun)(void);

Remember that in this case fun is a *pointer* to a function, not a
function itself.
Thanks & regards,
Shailendra


Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.