473,748 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

typedef help

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
2 2932
-----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)

iD8DBQFCsuO8agV FX4UWr64RAidKAJ 914YJdrpa/0czva+Ktlp9Ujfm LdQCfe8W6
1Cr9X6rWNi5G3ub h1Tl8GtI=
=wPU0
-----END PGP SIGNATURE-----
Nov 14 '05 #2

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 "declaratio n 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4519
by: Plok Plokowitsch | last post by:
After over a decade of programming in C++ I seem to have missed some substantial point (mental note: am I getting too old for this?). A little bit of help would be *very* appreciated. I'm trying to gather various different classes into a common namespace using typedefs: class QWidget {}; class MyListview {}; namespace gui
7
2147
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type y = x; but I can't understand the last one which is Array<int>::element_type y = x; We have a typedef T element_type; in the template Array class see below
3
4677
by: Blaless | last post by:
Hi, I have a problem compiling the next piece of code under Linux using g++ (both 3.4.3 and 4.0.1) template<typename T> class Table{ public: // typedef T (TBoxTest::*GetterType)(); typedef T (*Type)();
2
2346
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last line is very long. I'm wondering if there is any way to suppress it. I can only think of typedef. But I'm not sure whether I can use typedef for the return type. Would you please help me? Please don't be daunted by the length of the code.
4
3053
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum { DOUBLE_LIST, INT_LIST } DATA_TYPE; typedef struct { DATA_TYPE type;
2
3058
by: Immo Birnbaum | last post by:
Hi, I'm trying to solve a programming lab assignment for my college C programming course, but as they taught us two semesters of Java before teaching us any C, I'm having problems with all the aspects of pointers. I'd appreciate if anybody could help me with the following problem: I tried to learn how to use malloc, free, and the * and & operators. I started with a few simple lines of code like:
8
9306
by: Lathe_Biosas | last post by:
Hi While compiling my application that needs "windows.h" there are some typedef redefinition errors redefinition at winnt.h( line 207) typedef void *HANDLE redefinition at windef.h( line 143) typedef unsigned char BYTE redefinition at windef.h( line 141) typedef unsigned long DWORD #define INVALID_HANDLE_VALUE -1 redefinition at winbase.h(55) #define
1
3126
by: Thomas Barnet-Lamb | last post by:
I was wondering if anyone could give me some help with the following. Consider the code snippet: struct qqq{typedef qqq* pointer;}; template<class al> struct foo : public al { template <class T> struct rebind { typedef foo<qqq> other;
14
2252
by: yang__lee | last post by:
Hi, You all know typedef typedef struct g { int a; int b; } google;
5
1830
by: tissyrose | last post by:
Hi All, I am porting a very large application from VC++ 6.0 to VS 2005. There are many issues I am facing. One of them is I am getting an error "cannot access private typedef declared in class" . I removed the typedef then I am getting the following error. syntax error missing ';' before '*' Below is the code snippet..
0
8983
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
8822
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
9236
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
8235
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
6792
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
4863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3298
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
2
2774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
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.