In a C++ reference page they provide some typedef examples and I'm trying to understand what they mean.
// simple typedef typedef unsigned long mylong; // more complicated typedef typedef int int_t, *intp_t, (&fp)(int, mylong), arr_t[10];
So the simple typedef (the first declaration) I understand.
But what are they declaring with the second one (repeated below)?
typedef int int_t, *intp_t, (&fp)(int, ulong), arr_t[10];
Particularly what does (&fp)(int, mylong)
mean?
A typedef declaration lets you define your own identifiers that can be used in place of type specifiers such as int , float , and double . A typedef declaration does not reserve storage.
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.
A typedef declaration is a declaration with typedef as the storage class. The declarator becomes a new type. You can use typedef declarations to construct shorter or more meaningful names for types already defined by C or for types that you have declared.
The typedef is a keyword used in C programming to provide some meaningful names to the already existing variable in the C program. It behaves similarly as we define the alias for the commands. In short, we can say that this keyword is used to redefine the name of an already existing variable.
It's declaring several typedefs at once, just as you can declare several variables at once. They are all types based on int
, but some are modified into compound types.
Let's break it into separate declarations:
typedef int int_t; // simple int typedef int *intp_t; // pointer to int typedef int (&fp)(int, ulong); // reference to function returning int typedef int arr_t[10]; // array of 10 ints
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With