Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please explain syntax rules and scope for "typedef"

What are the rules? OTOH the simple case seems to imply the new type is the last thing on a line. Like here Uchar is the new type:

typedef unsigned char Uchar; 

But a function pointer is completely different. Here the new type is pFunc:

typedef int (*pFunc)(int); 

I can't think of any other examples offhand but I have come across some very confusing usages.

So are there rules or are people just supposed to know from experience that this is how it is done because they have seen it done this way before?

ALSO: What is the scope of a typedef?

like image 768
ValenceElectron Avatar asked Mar 11 '10 18:03

ValenceElectron


People also ask

What is the scope of a typedef?

A typedef is scoped exactly as the object declaration would have been, so it can be file scoped or local to a block or (in C++) to a namespace or class.

What is the syntax of typedef?

The syntax of typedef is as follows: Syntax: typedef data_type new_name; typedef : It is a keyword. data_type : It is the name of any existing type or user defined type created using structure/union. new_name : alias or new name you want to give to any existing type or user defined type.

What is typedef in C explain with example?

typedef is used to define new data type names to make a program more readable to the programmer. For example: | main() | main() { | { int money; | typedef int Pounds; money = 2; | Pounds money = 2 } | } These examples are EXACTLY the same to the compiler.


2 Answers

Basically a typedef has exactly the same syntax as an object declaration except that it is prefixed with typedef. Doing that changes the meaning of the declaration so that the new identifier declares an alias for the type that the object that would have been declared, had it been a normal declaration, would have had.

A typedef is scoped exactly as the object declaration would have been, so it can be file scoped or local to a block or (in C++) to a namespace or class.

e.g.

Declares an int:

int a; 

Declares a type that is an alias for int:

typedef int a_type; 

Declares a pointer to a char:

char *p; 

Declares an alias for a char *:

typedef char *pChar; 

Declares a function pointer:

int (*pFn)(int); 

Declares an alias for the type that is 'pointer to a function taking int and returning int':

typedef int (*pFunc)(int); 
like image 176
CB Bailey Avatar answered Sep 22 '22 21:09

CB Bailey


For syntactic convenience, typedef is treated as a storage class specifier, like extern, static, or register. Semantically of course it's quite different, but when typedef was added to the language, it was simpler to use an existing piece of the grammar to define its syntax.

Adding static to an object declaration doesn't change the meaning of the declaration except that it changes the object's storage class to "static" (if it wasn't already):

{     int foo; /* automatic storage duration */     static int bar; /* static storage duration */ } 

Replacing static with typedef changes the meaning of the declaration, so that the name being defined is not an object name but a type name (actually just an alias for an existing type):

    typedef int baz; /* "baz" is an alias for "int" */ 

The same syntax applies to more complex declarations:

int (*a)[42];         /* a is a pointer to an array of 42 ints */ static int (*b)[42];  /* Same as a, but with static storage duration */ typedef int (*c)[42]  /* c is an alias for the type int(*)[42], or                           "pointer to array of 42 ints" */ 

Once you realize that typedef was arbitrarily shoved into the same slot in the grammar occupied by extern, static, and register, understanding typedef declarations is no harder (and no easier!) than understanding object declarations. (The cdecl program and web site are useful for unpacking complex declarations.)

You can also have a typedef for a function type:

void func(void);              /* declare func as a function */ typedef void func_type(void); /* declare func_type as a name                                  for a function type */ 

You can't use a typedefed function type to declare or define a function, but you can use it to declare a function pointer:

func_type *ptr = func; 

As for scope (meaning the region of program text over which a declared identifier is visible), an identifier defined by a typedef declaration has the same scope as any other declared identifier. If it's declared at file scope, outside any function, it's visible from the point of declaration to the end of the file. If it's declared inside a function, it's visible from the point at which it's declared to the end of the nearest enclosing block. And like any declaration, it can be hidden by another declaration with the same name in an inner scope.

like image 26
Keith Thompson Avatar answered Sep 23 '22 21:09

Keith Thompson