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.

5.1 Arrays

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.


PIC

Figure 5.1: Array in memory


5.1.1 Array of integers

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.


PIC

Figure 5.2: Array of integers


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.


PIC

Figure 5.3: Stores square numbers in array


5.1.2 Advantages of Array in C

Arrays have a great significance in the C language. They provide several advantages to the programmers while programming. Some of them are:

5.1.3 String and Character 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,

char s[] = “home”;

contains 5 characters including the ’\0’ character which is automatically added by the compiler at the end of the string.


PIC

Figure 5.4: Char Array in memory


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).


PIC

Figure 5.5: Accessing Char Array from memory, and printing its addresses


5.1.4 Char String Handling Functions

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



PIC

Figure 5.6: String functions