ARRAYS
DEFINITION AND DECLARATION OF AN ARRAY :
- An ARRAY is a collection of a similar elements i.e collection of variables of same data-types.
- To declare an ARRAY in we must specify the type of the elements and the number of elements required by an array.
-
SYNTAX :
data_type array_name[size];
- The size must be an integer constant greater than zero and data_type can be any valid C datatype.
- This types of ARRAY are known as SINGLE-DIMENSION ARRAY.
- A specific element in an ARRAY is accessed by an index.
PROCESSING OF ARRAYS :
- If a and b are similar arrays (i.e., same data type, same dimensionality and same size), assignment operations, comparison operations, etc. must be carried out on an element-by-element basis.
- This is usually accomplished within a loop, where each pass through the loop is used to process one array element.
- The number of passes through the loop will therefore equal the number of array elements to be processed.
PASSING ARRAYS TO FUNCTIONS :
- We can pass entire array to a function as an argument.
- To pass an array to a function, the array name must appear by itself, without brackets or subscripts, actual argument within the function call.
- When declaring a one- dimensional array as a formal argument, the array name is written with a pair of empty square brackets.
- The size of the array is not specified within the formal argument declaration.
- An empty pair of square brackets must follow the name of each array argument, thus indicating that the argument is an array.
- If argument names are not included in a function declaration, then an empty pair of square brackets must follow the array argument data type.
MULTI-DIMENTIONAL ARRAYS :
- Multidimensional arrays are defined similar to one-dimensional arrays, except that a separate pair of square brackets is required for each subscript.
- Thus,a two-dimensional array will require two pairs of square brackets, a three-dimensional array will require three pairs of square brackets, and so on.
- Data in MULTI-DIMENTIONAL ARRAY is stored in tabular format.
-
SYNTAX :
data_type array_name[type 1][type 2]...[type n];
-
INITILIZING A MULTI-DIMENTINAL ARRAY :
int array1[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
OR
int array1[3][4]={ {1,2,3,4},{5,6,7,8},{9,10,11,12} };
ARRAYS AND STRINGS :
- strcmp() :
-
The strcmp() function accepts two strings as arguments and returns an integer value, depending upon the relative order of the two strings, as follows:
- A negative value if the first string precedes the second string alphabetically.
- A value of zero if the first stringand the second string are identical (disregardingcase).
- A positive value if the second string precedesthe first string alphabetically.
- Therefore, if strcmp( stri ngl , string2) returns a positive value, it would indicate that string2 must be moved, placing it ahead of stringl in order to alphabetize the two strings properly.
-
The strcmp() function accepts two strings as arguments and returns an integer value, depending upon the relative order of the two strings, as follows:
- strcpy() :
- The strcpy() function also accepts two strings as arguments.
-
If first argument is generally an identifier that represents a string.
The second argument can be a string constant or an identifier representing another string. - The function copies the value of string2to stringl. Hence, it effectively causesone string to be assigned to another.
Comments
Post a Comment