Arrays allow storing multiple values of the same type together in memory. A one-dimensional array stores elements in a linear list, accessed by index. A two-dimensional array stores elements in a grid, accessed by row and column indices. Arrays must be declared with a size and elements accessed using indices within bounds. Arrays can be initialized manually or with a loop. Multidimensional arrays generalize this to grids of any number of dimensions.
Arrays allow storing multiple values of the same type together in memory. A one-dimensional array stores elements in a linear list, accessed by index. A two-dimensional array stores elements in a grid, accessed by row and column indices. Arrays must be declared with a size and elements accessed using indices within bounds. Arrays can be initialized manually or with a loop. Multidimensional arrays generalize this to grids of any number of dimensions.
(FCCS002 ) Semester 1, Section 2 UNIT-II Arrays and Pointers Topic: Arrays Arrays: Introduction main( ) { int x ; x=5; x = 10 ;
printf ( "\n x = %d", x ) ;
} • per = { 48, 88, 34, 23, 96 }
• Thus, an array is a collection of similar elements.
• These similar elements could be all ints, or all floats, or all chars, etc. A Simple Program Using Array Array Declaration • type var_name[size]; • int marks[30] ; • double balance[100];
• The amount of storage required to hold an array is directly related to
its type and size. • For a single dimension array, the total size in bytes is computed as shown here: • total bytes = sizeof(base type) ×length of array Accessing Elements of an Array • Entering Data into an Array • Reading Data from an Array A Simple Program Using Array Array Initialisation Array Elements in Memory • Consider the following array declaration: int arr[8] ; Bounds Checking Strings • In C, a string is a null-terminated character array. (A null is zero.) • Thus, a string contains the characters that make up the string followed by a null. • The null-terminated string is the only type of string defined by C. • When declaring a character array that will hold a string, you need to declare it to be one character longer than the largest string that it will hold. • For example, to declare an array str that can hold a 10-character string, you would write • char str[11]; • Specifying 11 for the size makes room for the null at the end of the string. • C supports a wide range of functions that manipulate strings. The most common are listed here:
• These functions use the standard header <string.h>
Two-Dimensional Arrays • C supports multidimensional arrays. • The simplest form of the multidimensional array is the two dimensional array. • A two-dimensional array is, essentially, an array of one-dimensional arrays. • To declare a two-dimensional integer array d of size 10,20, you would write • int d[10][20]; • Similarly, to access point 1,2 of array d, you would use d[1] [2] • Here is a sample program that stores roll number and marks obtained by a student side by side in a matrix. • scanf ( "%d %d", &stud[i][0], &stud[i][1] ) ; Initialising a 2-Dimensional Array Memory Map of a 2-Dimensional Array
printf ( "Marks of third student = %d", stud[2][1] ) ;
• The following example loads a two-dimensional array with the numbers 1 through 12 and prints them row by row Multidimensional Arrays • type name[Size1][Size2][Size3] . . .[SizeN];