Hardware Reference
In-Depth Information
int average (int k, int brr[])
/* function definition */
{
. . .
}
Within main we see a call to the function average . This function call contains two
arguments—the integer variable n and the one-dimensional integer array arr . Note that arr
appears as an ordinary variable within the function call. In the first line of the function defi-
nition, we see two formal arguments, k and brr. The formal argument declarations establish
k as an integer variable and brr as a one-dimensional integer array. Note that the size of brr
is not defined in the function definition. As formal parameters in a function definition,
int brr[];
and
int *brr;
are equivalent.
5.7.5 Initializing Arrays
C allows the initialization of arrays. Standard data-type arrays may be initialized in a
straightforward manner. The syntax for initializing an array is
array_declarator 5 { value-list }
The following statement shows a fi ve-element integer array initialization:
int arr[5] 5 {10, 20, 30, 40, 50};
The element arr[0] has the value of 10 and the element arr[4] has the value of 50.
A string (character array) can be initialized in two ways. One method is to make a list of
each individual character:
char strgx[5] 5 {'w', 'x', 'y', 'z', 0};
The second method is to use a string constant.
char myname [6] 5 “Edison”;
A null character is automatically appended at the end of “Edison.” When initializing an
entire array, the array size (which is one more than the actual length) must be included.
char prompt [24] 5 “Please enter an integer:”;
5.7.6 Structures
A structure is a group of related variables that can be accessed through a common name.
Each item within a structure has its own data type, which can be different from those of the
other data items. The syntax of a structure declaration is
struct struct_name {
/* struct_name is optional */
type1
member1;
type2
member2;
. . .
};
The struct_name is optional and, if it exists, defines a structure tag . A struct declaration
defines a type. The right brace that terminates the list of members may be followed by a list of
variables, just as for any basic type. The following example is for a card catalog in a library:
struct catalog_tag {
char
author [40];
char
title [40];
char
pub [40];
 
Search WWH ::




Custom Search