| C Tutorial | Pointer dereferencing | |||
|
Data types in C
|
We'll see shortly how a pointer is set to point to something -- for now just assume the pointer points to memory of the appropriate type. In an expression, the unary * to the left of a pointer dereferences it to retrieve the value it points to. The following drawing shows the types involved with a single pointer pointing to a struct fraction. struct fraction* f1;
There's an alternate, more readable syntax available for dereferencing a pointer to a struct. A "->" at the right of the pointer can access any of the fields in the struct. So the reference to the numerator field could be written f1->numerator.
Here are some more complex declarations... struct fraction** fp; // a pointer to a pointer to a struct fraction struct fraction fract_array[20]; // an array of 20 struct fractions struct fraction* fract_ptr_array[20]; // an array of 20 pointers to // struct fractions
One nice thing about the C type syntax is that it avoids the circular definition problems which come up when a pointer structure needs to refer to itself. The following definition defines a node in a linked list. Note that no preparatory declaration of the node pointer type is necessary.
struct node { int data; struct node* next; };
Want To Know more with Video ??? |
|