This is an old revision of the document!
Corning Community College
CSCS2320 Data Structures
~~TOC~~
To review structs and pointers, and see how these two concepts, when combined, produces the core element of our class explorations.
As we learned in C, there are two main composite data types available to us:
The struct effectively lets us design our own data type, by filling a container with all the types we need to aid us in solving some problem more effectively.
And what's more, structs are the basis for classes (they are essentially structs with some relaxed rules (i.e. they can have functions) and additional syntax and abilities (constructors, access control).
Structure elements are accessed with either the “.” or “->” operator, depending on the declared status of the struct.
Structs declared as pointers use the “→” (I commonly see it called the structure pointer), where non-pointered structs use the “.”
For example:
struct rectangle { int length; int width; float area; };
Can be used as a non-pointer instance:
struct rectangle box; box.length = 12; box.width = 10; box.area = box.length * box.width;
or as a pointer:
struct rectangle *box; box = (struct rectangle *) malloc (sizeof(struct rectangle)); box -> length = 12; box -> width = 10; box -> area = box -> length * box -> width;