Expert C Programming

(Jeff_L) #1

ptr_to_func signal(int, ptr_to_func);


/* this says that signal is a function that takes


* two arguments, an int and a ptr_to_func, and


* returns a ptr_to_func


*/


Typedef is not without its drawbacks, however. It has the same confusing syntax of other declarations,
and the same ability to cram several declarators into one declaration. It provides essentially nothing


for structs, except the unhelpful ability to omit the struct keyword. And in any typedef, you


don't even have to put the typedef at the start of the declaration!


Handy Heuristic


Tips for Working with Declarators


Don't put several declarators together in one typedef, like this:


typedef int ptr, (fun)(), arr[5];


/* ptr is the type "pointer to int"


* fun is the type "pointer to a function returning int"


* arr is the type "array of 5 ints"


*/


And never, ever, bury the typedef in the middle of a declaration, like this:


unsigned const long typedef int volatile *kumquat;


Typedef creates aliases for data types rather than new data types. You can typedef any type.


typedef int (*array_ptr)[100];


Just write a declaration for a variable with the type you desire. Have the name of the variable be the
name you want for the alias. Write the keyword 'typedef ' at the start, as shown above. A typedef name
cannot be the same as another identifier in the same block.

Free download pdf