Tutorial by Examples

Structure data types are useful way to package related data and have them behave like a single variable. Declaring a simple struct that holds two int members: struct point { int x; int y; }; x and y are called the members (or fields) of point struct. Defining and using structs: ...
Combining typedef with struct can make code clearer. For example: typedef struct { int x, y; } Point; as opposed to: struct Point { int x, y; }; could be declared as: Point point; instead of: struct Point point; Even better is to use the following typedef struct Poin...
When you have a variable containing a struct, you can access its fields using the dot operator (.). However, if you have a pointer to a struct, this will not work. You have to use the arrow operator (->) to access its fields. Here's an example of a terribly simple (some might say "terribl...
C99 Type Declaration A structure with at least one member may additionally contain a single array member of unspecified length at the end of the structure. This is called a flexible array member: struct ex1 { size_t foo; int flex[]; }; struct ex2_header { int foo; char...
In C, all arguments are passed to functions by value, including structs. For small structs, this is a good thing as it means there is no overhead from accessing the data through a pointer. However, it also makes it very easy to accidentally pass a huge struct resulting in poor performance, particula...
Structs may be used to implement code in an object oriented manner. A struct is similar to a class, but is missing the functions which normally also form part of a class, we can add these as function pointer member variables. To stay with our coordinates example: /* coordinates.h */ typedef stru...

Page 1 of 1