Next: Inheritance Up: New Concepts Previous: New Data Types

Pointers

In C/C++ , there is a powerful way to access variables, through pointers. Suppose we have a big, complicated FARFALLA tree in memory already, and the programmer wants to access the data variables in a particular node in the tree. There are a few ways to this. One way would be to make a copy of the node that he or she wanted into a local copy of the same type of node. To do this, the programmer needs to make a local variable of the proper type to copy the nodes into.

Instead, it is possible to just get the address in memory of the node in the tree(in FARFALLA functions exist to do this) and refer to variables through that address. Now we no longer need a local copy of the variable, but we do need a way to point at the data if we have the memory address of the node. That is what pointers are for. A pointer is just one word containing an address in memory.

To make use of this feature, we must declare a pointer. A variable to hold the address of an erp variable is declared like this: erp *erpPtr;. The `*' means that the variable called erpPtr is a pointer to an erp, not an erp. To access the data members of the erp variable the erpPtr points to, we use the special notation erpPtr->adc0u. Here is an example:


erp *erpPtr;
some_function(input_variables,erpPtr); // makes erpPtr point at some 
                                       // particular erp node in memory
                                       // that we want.

adc    = erpPtr->adc0u;                // Access adc0u through the pointer
energy = erpPtr->energy;               // Access energy through the pointer

Pointers may be difficult to understand at first, but they are one of the most powerful features of C/C++ programming. You can learn some more about pointers in the FARFALLA Programming Reference Guide.


walter@
Wed Aug 10 11:42:05 PDT 1994