HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 5 Expressions and Operators

Array Subscripting ([ ])

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

A postfix expression followed by the [ ] operator is a subscripted reference to a single element in an array.

Syntax

postfix-expression [ expression ]

Description

One of the operands of the subscript operator must be of type pointer to T (T is an object type), the other of integral type. The resulting type is T.

The [ ] operator is defined so that E1[E2] is identical to (*((E1)+(E2))) in every respect. This leads to the (counterintuitive) conclusion that the [ ] operator is commutative. The expression E1[E2] is identical to E2[E1].

C's subscripts run from 0 to n-1 where n is the array size.

Multidimensional arrays are represented as arrays of arrays. For this reason, the notation is to add subscript operators, not to put multiple expressions within a single set of brackets. For example, int x[3][5] is actually a declaration for an array of three objects. Each object is, in turn, an array of five int. Because of this, all of the following expressions are correct:

x
x[i]
x[i][j]

The first expression refers to the 3 by 5 array of int. The second refers to an array of five int, and the last expression refers to a single int.

The expression x[y] is an lvalue.

There is no arbitrary limit on the number of dimensions that you can declare in an array.

Because of the design of multidimensional C arrays, the individual data objects must be stored in row-major order.

As another example, the expression

a[i,j] = 0

looks as if array a were doubly subscripted, when actually the comma in the subscript indicates that the value of i should be discarded and that j is the subscript into the a array.

© Hewlett-Packard Development Company, L.P.