5.7. Expressions involving pointersBecause of the introduction of qualified types and of the notion of
incomplete types, together with the use of You don't yet know the Standard means when it talks about objects or incomplete types. So far we have tended to use the term loosely, but properly speaking an object is a piece of data storage whose contents is to be interpreted as a value. A function is not an object. An incomplete type is one whose name and type are mostly known, but whose size hasn't yet been determined. You can get these in two ways:
There will be some more discussion of incomplete types in later chapters. Now for what you are allowed to do with pointers. Note that wherever we
talk about qualified types they can be qualified with 5.7.1. ConversionsPointers to
int i;
int *ip;
void *vp;
ip = &i;
vp = ip;
ip = vp;
if(ip != &i)
printf("Compiler error\n");
An unqualified pointer type may be converted to a qualified pointer type, but the reverse is not true. The two values will be equal:
int i;
int *ip, *const cpi;
ip = &i;
cpi = ip; /* permitted */
if(cpi != ip)
printf("Compiler error\n");
ip = cpi; /* not permitted */
A null pointer constant (see earlier) will not be equal to a pointer to any object or function. 5.7.2. ArithmeticExpressions can add (or subtract, which is equivalent to adding
negative values) integral values to the value of a pointer to any object
type. The result has the type of the pointer and if n is
added, then the result points n array elements away from the
pointer. The most common use is repeatedly to add It the pointer resulting from the addition points in front of the array or past the non-existent element just after the last element of the array, then you have had overflow or underflow and the result is undefined. The last-plus-one element of an array has always been assumed to be a valid address for a pointer and the Standard confirms this. You mustn't actually access that element, but the address is guaranteed to exist rather than being an overflow condition. We've been careful to use the term ‘expression’ rather than
saying that you actually add something to the pointer itself. You can do
that, but only if the pointer is not qualified with Two pointers to compatible types whether or not qualified
may be subtracted. The result has the type
int x[100];
int *pi, *cpi = &x[99]; /* cpi points to the last element of x */
pi = x;
if((cpi - pi) != 99)
printf("Error\n");
pi = cpi;
pi++; /* increment past end of x */
if((pi - cpi) != 1)
printf("Error\n");
5.7.3. Relational expressionsThese allow us to compare pointers with each other. You can only compare
It does not matter if the types that are pointed to are qualified or unqualified. If two pointers compare equal to each other then they point to the same thing, whether it is an object or the non-existent element off the end of an array (see arithmetic, above). If two pointers point to the same thing, then they compare equal to each other. The relational operators >, <= and so on all give the result that you would expect if the pointers point into the same array: if one pointer compares less than another, then it points nearer to the front of the array. A null pointer constant can be assigned to a pointer; that pointer will then compare equal to the null pointer constant (which is pretty obvious). A null pointer constant or a null pointer will not compare equal to a pointer that points to anything which actually exists. 5.7.4. AssignmentYou can use pointers with the assignment operators if the following conditions are met:
In the last two cases, the type pointed to by the left-hand side must have at least the same qualifiers as the type pointed to by the right-hand side (possibly more). So, you can assign a pointer to int to a pointer to The 5.7.5. Conditional operatorThe description of the behaviour of this operator when it is used with pointers has already been given in Chapter 3. |
The C BookThis book is published as a matter of historical interest. Please read the copyright and disclaimer information. GBdirect Ltd provides up-to-date training and consultancy in C, Embedded C, C++ and a wide range of other subjects based on open standards if you happen to be interested. |
|
West Yorkshire Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday South East Regional Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday Please note: |