Skip to content
Physics and Astronomy
Home Our Teaching Resources C programming Appendix: some more details
Back to top
On this page
Contents

Appendix: Some minor points about structures and typedef.

Definition and declaration can be combined.

It's possible, although extremely rare, to combine the definition and declaration together. Here we define a structure type and declare an actual structure, mystruct:

int main() {
 
  // More stuff here..
(Note no word "typedef").

Structures can be initialised at declaration

So to continue the previous example we may declare another structure and initialise it:

 

Any declaration can be made into a typedef

Sharp-eyed readers may have noticed that our example typical structure typedef in the main notes:

#define MAXLEN 64
typedef struct nuclide {
  char name[MAXLEN];
  double halflife;
} Nuclide;

has exactly the same form as the combined struction definition above, with the word "typedef" before it. This is a general rule: we may take any valid variable declaration and put the word "typedef" in front of it. When we do this we are not declaring a new variable but a new alias.

Examples

Example Meaning
Float is now an alias for double
y is a double
 
Example Meaning
coord is an alias for an array of three doubles
x is an array of three doubles
Typical use of the array x
 
Example Meaning
Float is now an alias for double
Position is an alias for an array of three Floats, i.e. three doubles
z is an array of three doubles
Typical use of the array z
Log in
                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Privacy & cookies Share
Back to top