by Dinesh Thakur Category: Array

Declaring Arrays : Arrays are declared with the bracket punctuators [ ], as shown in the following syntax :

storage-class-specifier(opt) type-specifier declarator [constant-expression-list(opt)];

The following example shows a declaration of a 10-element array of integers, a

variable called table_one :

int table_one[10];


The type-specifier shows the data type of the elements. The elements of an array can be of any scalar or aggregate data type. The identifier table_one specifies the name of the array. The constant expression 10 gives the number of elements in a single dimension. Arrays in C are zero-based; that is, the first element of the array is identified with a 0 subscript, such as the one shown in the following example :


int x[5];

x[0] = 25; /* The first array element is assigned the value 25 */


The expression between the brackets in the declaration must be an integral constant expression with a value greater than zero. Omitting the constant expression creates an incomplete array declaration, which is useful in the following cases :


  • If the array is declared external and its storage is allocated by a definition in another place, you can omit the constant expression for convenience when the array name is declared, as in the following example :


extern int array1[];

int first_function(void)






In a separate compilation unit :

int array1[10];

int second_function(void)






The array size specifier may only be omitted from the first pair of brackets in a multidimensional array declaration. This is because an array's elements must have complete types, even if the array itself has an incomplete type.


  • If the declaration of the array includes initializers, you can omit the size of the array, as in the following example :


char array_one[] = "Shemps";

char array_two[] = { 'S', 'h', 'e', 'm', 'p', 's', '\0' };


The two definitions initialize variables with identical elements. These arrays have seven elements: six characters and the null character (\0), which terminates all character strings. The size of the array is determined from the number of characters in the initializing character-string constant or initialization list. Initializing an incomplete array completes the array type. An array is completed at the end of its initializer list.


  • If you use the array as a function parameter, the array must be defined in the calling function. However, the declaration of the parameter in the called function can omit the constant expression within the brackets. The address of the first element of the array is passed. Subscripted references in the called function can modify elements of the array. The following example shows how to use an array in this manner :




/* Initialize array */

static char arg_str[] = "Thomas";

int sum;

sum = adder(arg_str); /* Pass address of first array element */





/* adder adds ASCII values of letters in array */

int adder( char param_string[])


int i, sum = 0; /* Incrementer and sum */

/* Loop until NULL char */

for (i = 0; param_string[i] != '\0'; i++)

sum += param_string[i];


return sum;


After the function adder is called, parameter param_string receives the address of the first character of argument arg_str, which can then be accessed in adder . The declaration of param_ string serves only to give the type of the parameter, not to reserve storage for it.


Array members can also be pointers. The following example declares an array of floating-point numbers and an array of pointers to floating-point numbers :


float fa[11], *afp[17];


When a function parameter is declared as an array, the compiler treats the declaration as a pointer to the first element of the array. For example, if x is a parameter and is intended to represent an array of integers, it can be declared as any one of the following declarations :


int x[];

int *x;

int x[10];


Note that the specified size of the array does not matter in the case of a function parameter, since the pointer always points to only the first element of the array.


C supports arrays declared as an array of arrays. These are sometimes called multidimensional arrays. Consider the following example, where variable table_one is a two-dimensional array containing 20 integers :


int table_one[10][2];


Arrays are stored in row-major order, which means the element table_one[0][0] (in the previous example) immediately precedes table_one[0][1], which in turn immediately precedes table_one[1][0] .


About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles