Type Qualifiers 379
register int index;
register char *textPtr;
Both local variables and formal parameters can be declared as registervariables.The
types of variables that can be assigned to registers vary among machines.The basic data
types can usually be assigned to registers, as well as pointers to any data type.
Even if your compiler enables you to declare a variable as a registervariable, it is
still not guaranteed that it will do anything with that declaration. It is up to the
compiler.
You might want to also note that you cannot apply the address operator to a
registervariable. Other than that,registervariables behave just as ordinary automatic
variables.
The volatileQualifier
This is sort of the inverse to const. It tells the compiler explicitly that the specified vari-
able willchange its value. It’s included in the language to prevent the compiler from
optimizing away seemingly redundant assignments to a variable, or repeated examination
of a variable without its value seemingly changing. A good example is to consider an
I/O port. Suppose you have an output port that’s pointed to by a variable in your pro-
gram called outPort. If you want to write two characters to the port, for example an O
followed by an N, you might have the following code:
outPort = 'O';
outPort = 'N';
A smart compiler might notice two successive assignments to the same location and,
because outPortisn’t being modified in between, simply remove the first assignment
from the program.To prevent this from happening, you declare outPortto be a
volatilepointer, as follows:
volatile char *outPort;
The restrictQualifier
Like the registerqualifier,restrictis an optimization hint for the compiler. As such,
the compiler can choose to ignore it. It is used to tell the compiler that a particular
pointer is the only reference (either indirect or direct) to the value it points to through-
out its scope.That is, the same value is not referenced by any other pointer or variable
within that scope.
The lines
int restrict intPtrA;
int restrict intPtrB;
tell the compiler that, for the duration of the scope in which intPtrAand intPtrBare
defined, they will never access the same value.Their use for pointing to integers inside
an array, for example, is mutually exclusive.