6.3 Postfix Operators

Postfix expressions include array references, function calls, structure or union references, and postfix increment and decrement expressions. The operators in postfix expressions have left-to-right associativity.

Postfix expressions have the following syntax:

postfix-expression:

   array-reference
   function-call
   structure-and-union-member-reference
   postfix-increment-expression
   postfix-decrement-expression

6.3.1 Array References

The bracket operator [ ] is used to refer to an element of an array. Array references have the following syntax:

array-reference:

   postfix-expression [ expression ]

For example, in a one-dimensional array, you can refer to a specific element within the array as follows:

int sample_array[10];  /* Array declaration; array has 10 elements */
sample_array[0] = 180; /* Assign value to first array element      */

This example assigns a value of 180 to the first element of the array, sample_array[0] . Note that C uses zero-origin array subscripting.

In a two-dimensional array (more properly termed an array of arrays), you can refer to a specific element within the array, as follows:

int sample_array[10][5];  /* Array declaration; array has 50 elements */
sample_array[9][4] = 180; /* Assign value to last array element       */

This example assigns a value of 180 to the element sample_ array[9][4] .

Conceptually, multidimensional arrays are of type arrays of arrays of arrays .... Therefore, if an array reference is not fully qualified, it refers to the address of the first element in the dimension that is not specified. For example:

int sample_array[10][5]; /* Array declaration                      */
int *p1;                 /* Pointer declaration                    */

p1 = sample_array[7];    /* Assigns address of subarray to pointer */

In this example, p1 contains the address of the first element in the one-dimensional subarray sample_array[7] . Although, as in this example, a partially qualified array can be used as an rvalue, only a fully qualified array reference can be used as an lvalue. For example, C does not allow the following statement, in which the second dimension of the array is omitted:

int sample_array[10][5]; /* Array declaration                      */

sample_array[7] = 21;    /* Error                                  */

A reference to an array name with no bracket can be used to pass the array's address to a function, as in the following statement:

funct(sample_array);

Bracket operators can also be used to perform general pointer arithmetic as follows:

p1[intexp]

Here, p1 is a pointer and intexp is an integer-valued expression. The result of the expression is the value pointed to by p1 incremented by the value of intexp multiplied by the size, in bytes, of the addressed object (array element). The expressions * (p1 + intexp) and p1[intexp] are defined to be equivalent; both expressions refer to the same memory location and have the same type. Array subscripting is a commutative operation: intexp[p1] is equivalent to p1[intexp] . A subscripted expression is always an lvalue.

6.3.2 Function Calls

Function calls have the following syntax:

function-call:

   postfix-expression ( argument-expression-
   list(opt) )

argument-expression-list(opt):

   assignment-expression
   argument-expression-list(opt), assignment-
   expression

A function call is a postfix expression consisting of a function designator followed by parentheses. The order of evaluation of any expressions in the function parameter list is undefined, but there is a sequence point before the actual call. The parentheses can contain a list of arguments (separated by commas) or can be empty. If the function called has not been declared, it is assumed to be a function returning int .

To pass an argument that is an array or function, specify the identifier in the argument list without brackets or parentheses. The compiler passes the address of the array or function to the called routine, which means that the corresponding parameters in the called function must be declared as pointers.

In the following example, func1 is declared as a function returning double ; the number and type of the parameters are not specified:

double func1();

The function func1 can then be used in a function call, as follows:

result = func1(c);
      or
result = func1();

The identifier func1 can also be used in other contexts, without the parentheses. For example, as an argument to another function call:

dispatch(func1);

In this example, the address of the function func1 is passed to the function dispatch . In general, if an identifier is declared as a "function returning . . . " type, it is converted to "the address of function returning . . . " when that identifier is passed as an argument without its parentheses; the only exception is when the function designator is the operand of the unary & operator, in which case this conversion is explicit.

Functions can also be called by dereferencing a pointer to a function. In the following example, pf is declared as a pointer to a function returning double and assigned the address of the function func1 :

double (*pf)( );
   .
   .
   .
pf = func1;

The function func1 can then be called as follows:

result = (*pf)();

Although this function call is valid, the following form of the same function call is simpler:

result = pf();

In function calls, if the expression that denotes the called function has a type that does not include a prototype, the integral promotions discussed in Section 6.10.3 are performed on each applicable argument, and arguments that have type float are converted to double . These are called the default argument promotions. If the number of passed arguments does not agree with the number of parameters, the behavior is undefined. If the function is defined with a type that does not include a prototype, and the types of the arguments after promotion are not compatible with the types of the parameters after promotion, the behavior is undefined. If the function is defined with a type that includes a prototype, and the types of the arguments after promotion are not compatible with the types of the parameters, or if the prototype ends with an ellipsis punctuator (indicating a variable-length parameter list), the behavior is undefined.

If the expression that denotes the called function has a type that includes a prototype, the passed arguments are implicitly converted to the types of the corresponding parameters. The ellipsis punctuator in a function prototype causes argument type conversion to stop after the last declared parameter. The default argument promotions are performed on trailing arguments. If the function is defined with a type that is not compatible with the type pointed to by the expression that denotes the called function, the behavior is undefined.

No other conversions are implicitly performed; in particular, the number and types of arguments are not compared with those of the parameters in a function definition that does not include a prototype.

Recursive function calls are permitted, both directly and indirectly through any chain of other functions.

6.3.3 Structure and Union References

A member of a structure or union can be referenced either directly using the dot (.) operator, or indirectly using the arrow (->) operator.

Structure and union references (also called component selections) have the following syntax:

structure-and-union-reference:

   postfix-expression . identifier
   postfix-expression -> identifier

The arrow operator always produces an lvalue. The dot operator produces an lvalue if the postfix expression is an lvalue.

In a direct member selection, the first operand must designate a structure or union, and the identifier must name a declared member of that structure or union.

In an indirect member selection, the first operand must be a pointer to a structure or union, and the identifier must name a declared member of that structure or union. The arrow operator is specified with a hyphen (-) and a greater-than symbol (>) and designates a reference to the structure or union member. The expression E1->name is, by definition, precisely the same as (*E1).name. This also implies that E2.name is the same as (&E2)->name, if E2 is an lvalue.

A named structure member must be fully qualified; that is, it must be preceded by a list of the names of any higher-level members separated by periods, arrows, or both. The value of the expression is the named member of the structure or union, and its type is the type of that member. For more information about structures and unions, see Sections 3.4.4 and 3.4.5.

With one exception, if a member of a union is accessed after a value has been stored in a different member of that union, the result is dependent on the data types of the members referenced and their alignment within the union.

The exception exists to simplify the use of unions. If a union contains several structures that share a common initial sequence, and if the union currently contains one of these structures, you can inspect the common initial part of any of them. Two structures share a common initial sequence if corresponding members have compatible types (and for bit fields, the same width) for a sequence of one or more initial members.

6.3.4 Postfix Increment and Decrement Operators

C has two unary operators for incrementing and decrementing objects of scalar type. Postfix incrementation has the following syntax:

postfix-increment-expression:

   postfix-expression ++

Postfix decrementation has the following syntax:

postfix-decrement-expression:

   postfix-expression - -

The increment operator ++ adds 1 to its operand, and the decrement operator - subtracts 1, except when the operand is a pointer. If the operand is a pointer of type pointer to T, the pointer is incremented (or decremented) by sizeof (T). The effect is to point to the next (or previous) element within an array of objects of type T.

Both ++ and - can be used either as prefix operators (before the operand: ++n ) or postfix operators (after the operand: n++ ). In both cases, the effect is to increment n. The expression ++n increments n before its value is used, while n++ increments n after its value is used.

Section 6.4.3 describes the prefix form of the increment and decrement operators. This section addresses the postfix form.

Consider the following expression:

lvalue++

The postfix operator ++ adds the constant 1 to the operand, modifying the operand. The value of the expression is the value of the operand incremented by 1; otherwise, the result of the expression is the old value of the operand, before it was incremented. For example:

int i, j;
j = 5;
j++;                   /* j = 6 (j incremented by 1) */
i = j++;               /* i = 6, j = 7               */

When using the increment and decrement operators, do not depend on the order of evaluation of expressions. Consider the following ambiguous expression:

k = x[j] + j++;

It is unspecified whether the value of j in x[j] is evaluated before or after j is incremented. To avoid ambiguity, increment the variable in a separate statement, as in the following example:

j++;
k = x[j] + j;

The ++ and - operators can also be used with floating-point objects. In this case they scale the object by 1.0.


Previous Page | Next Page | Table of Contents | Index