next up previous contents
Next: Classes and minimal memory Up: Some basics of object-oriented Previous: Pointers   Contents

Structs

Plain C allows things like


{}
struct Node {
    int id ;
    double xCoord ;
    double yCoord ;
};
This means that our node has properties, such as an ID number and coordinates. These are used as follows:


{}
struct Node node ;
...
node.id = 213 ; //assingment of ID number 213
xx = node.xCoord ; // retrieval of xCoord

Typically, this is however used in pointer syntax; the example then is


{}
// this does not work yet, see text
struct Node* node ;
...
node->id = 213 ; 
xx = node->xCoord ;
Note that the arrow -> comes from converting Node node into Node* node. That is, arrows mean that the thing to the left of them is a pointer.

There is not yet a big advantage of using it this way. If one looks at the memory management, then struct Node* node only reserves space for the memory address itself; we would however also need memory space for id, xCoord, yCoord, which we don't have at this level. This will be solved in the next paragraph.



2004-02-02