Intro to Programming Week 6
Intro to Programming Week 6
Contents
1 Introduction to Arrays 3
1.1 Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Uses of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4 Arrays in Functions 5
4.1 Passing Arrays to Functions . . . . . . . . . . . . . . . . . . . . . 5
4.1.1 Function Declaration . . . . . . . . . . . . . . . . . . . . . 5
4.1.2 Function Definition . . . . . . . . . . . . . . . . . . . . . . 5
4.1.3 Function Call . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.2 Multidimensional Arrays in Functions . . . . . . . . . . . . . . . 6
4.2.1 Function Declaration . . . . . . . . . . . . . . . . . . . . . 6
4.2.2 Function Definition . . . . . . . . . . . . . . . . . . . . . . 6
4.2.3 Function Call . . . . . . . . . . . . . . . . . . . . . . . . . 6
1
5 String Manipulation and Functions 6
5.1 Definition of Strings . . . . . . . . . . . . . . . . . . . . . . . . . 6
5.2 String Declaration and Initialization . . . . . . . . . . . . . . . . 7
5.3 Common String Operations . . . . . . . . . . . . . . . . . . . . . 7
5.3.1 String Length . . . . . . . . . . . . . . . . . . . . . . . . . 7
5.3.2 String Concatenation . . . . . . . . . . . . . . . . . . . . 7
5.3.3 String Comparison . . . . . . . . . . . . . . . . . . . . . . 7
5.3.4 String Copy . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5.4 String Manipulation Functions . . . . . . . . . . . . . . . . . . . 8
5.4.1 strcpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.4.2 strcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.4.3 strcmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.4.4 strlen . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
6 Conclusion 9
2
1 Introduction to Arrays
1.1 Definition
An array is a data structure consisting of a collection of elements, each identified
by at least one array index or key. Arrays are used to store multiple values in
a single variable, instead of declaring separate variables for each value.
1.2 Characteristics
• Fixed Size: The size of an array is determined at the time of its creation.
• Homogeneous Elements: All elements in an array are of the same data
type.
• Contiguous Memory Location: Elements are stored in contiguous
memory locations.
1 // Declaration
2 int arr [10];
3
4 // Initialization
5 int arr [5] = {1 , 2 , 3 , 4 , 5};
3
2.2 Multidimensional Arrays
Multidimensional arrays are arrays of arrays. The most commonly used multi-
dimensional arrays are two-dimensional arrays.
1 // Declaration
2 int arr [3][4];
3
4 // Initialization
5 int arr [2][3] = {{1 , 2 , 3} , {4 , 5 , 6}};
4
3.1.2 Pointer Arithmetic
4 Arrays in Functions
4.1 Passing Arrays to Functions
Arrays can be passed to functions by specifying the array name without brackets.
The function receives a pointer to the first element of the array.
5
4.1.3 Function Call
1 int main () {
2 int arr [5] = {1 , 2 , 3 , 4 , 5};
3 printArray ( arr , 5) ;
4 return 0;
5 }
1 int main () {
2 int arr [2][3] = {{1 , 2 , 3} , {4 , 5 , 6}};
3 print2DArray ( arr , 2) ;
4 return 0;
5 }
6
5.2 String Declaration and Initialization
1 // Declaration
2 char str [10];
3
4 // Initialization
5 char str [] = " Hello ";
7
4 char str2 [] = " Hello ";
5 strcpy ( str1 , str2 ) ; // str1 is now " Hello "
5.4.2 strcat
Appends a copy of the source string to the destination string.
1 # include < string .h >
2
5.4.3 strcmp
Compares two strings lexicographically.
1 # include < string .h >
2
5.4.4 strlen
Computes the length of the string.
1 # include < string .h >
2
8
6 Conclusion
Arrays and strings are fundamental data structures in programming. Under-
standing their properties, how to manipulate them, and the functions available
for their manipulation is essential for efficient and effective coding. The conver-
gence between arrays and pointers is particularly important in C programming,
as it provides a deeper understanding of memory management and efficient data
handling.