Computer Fundamentals with C and Unix
(Arrays)
Lecture 05: Jan. 03, 2023 Prof. K.R. Chowdhary : Professor of CS
Disclaimer: These notes have not been subjected to the usual scrutiny reserved for formal publications. They may be distributed outside this class only with the permission of the Instructor.
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To create an array, define the data type (like int ) and specify the name of the array followed by square brackets [].
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows:
type arrayName [arraySize];
This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid C data type.
If we have a small number of elements, let us say we want 3 variables, then we can declare them separately like var1, var2, and var3. But if we have a large number of variables then we can use arrays to store them.
You can declare an array of any data type (i.e. int, float, double, char) in C. The following ways can be used to declare and initialize an array in C. Array can be of type: char, int, long int, double. In fact any type declaration can be in the form of an array.
Arrays have a great significance in the C language. They provide several advantages to the programmers while programming. Some of them are:
Arrays make the code more optimized and clean since we can store multiple elements in a single array at once, so we do not have to write or initialize them multiple times.
Every element can be traversed in an array using a single loop.
Arrays make sorting much easier. Elements can be sorted by writing a few lines of code.
Any array element can be accessed in any order either from the front or rear in O(1) time.
Insertion or deletion of the elements can be done in linear complexity in an array.
String is a sequence of characters that are treated as a single data item and terminated by a null character ’\0’. Remember that the C language does not support strings as a data type. A string is actually a one-dimensional array of characters in C language. These are often used to create meaningful and readable programs.
For example: The string ”home” declared by,
contains 5 characters including the ’\0’ character which is automatically added by the compiler at the end of the string.
The Fig. 5.5 shows that char array s[] stores h, o, m, e in the array elements s[0], s[1], s[2], s[3], respectively, and stores ‘\0’ in the element s[4]. The corresponding memory locations are also shown in this figure, not that difference between memory addresses is 1 for character elements. The %p is format specifier for pointer (pointer in C language means address of some memory location).
C language supports a large number of string handling functions that can be used to carry out many of the string manipulations. These functions are packaged in the string.h library. Hence, you must include string.h header file in your programs to use these functions.
The following are the most commonly used string handling functions.
Method | Description |
strcat() | It is used to concatenate(combine) two strings |
strlen() | It is used to show the length of a string |
strcpy() | Copies one string into another |
strcmp() | It is used to compare two string |