Array

Array

In C programming, an array is a collection of variables of the same data type stored in contiguous memory locations. Here is how to declare, initialize, and access an array in C:

Declaring an array

To declare an array in C, you use the following syntax:

				
					Datatype arrayname[array_size];
				
			

For example, to declare an array of integers with 5 elements, you would use:

				
					int numbers[5]; 
				
			

Initializing an array

You can initialize an array in C using curly braces {}. Here is an example of initializing an array of integers:

				
					int numbers[5] = {1, 2, 3, 4, 5}; 
				
			

You can also leave out the array size when initializing an array, and the compiler will automatically determine the size based on the number of elements in the initialization list:

				
					int numbers[] = {1, 2, 3, 4, 5}; 
				
			

If you initialize only a portion of the array, the remaining elements will be set to 0:

				
					int numbers[5] = {1, 2, 3}; 
				
			

In this example, numbers[0] is initialized to 1, numbers[1] is initialized to 2, numbers[2] is initialized to 3, and numbers[3] and numbers[4] are set to 0.

Accessing  array  elements

You can access individual elements of an array using the following syntax:

				
					arrayname[index] 
				
			

For example, to access the third element of the numbers array declared earlier, you would use:

				
					int  third_number = numbers[2]; 
				
			

In C, arrays are zero-indexed, which means the first element is at index 0, the second element is at index 1, and so on.

In summary, you declare an array in C by specifying the data type and size of the array using square brackets []. You can initialize an array by providing a list of values inside curly braces {}, or you can leave the array uninitialized. To access individual elements of an array, you use the array name followed by the element index enclosed in square brackets [].

Multidimensional  Arrays

In C programming, a multidimensional array is an array with more than one dimension, which means it has multiple rows and columns. The syntax for declaring and initializing a multidimensional array is as follows:

				
					datatype arrayname[dimension1][dimension2]...[dimensionN]; 
				
			

For example, to declare a two-dimensional array with 3 rows and 4 columns of integers, you would use:

				
					int numbers[3][4]; 
				
			

This creates an array with 3 rows and 4 columns, where each element can be accessed using two indices: the row index and the column index.

You can initialize a multidimensional array in a similar way to a one-dimensional array, using nested curly braces {}. Here is an example of initializing a two-dimensional array:

				
					int numbers[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; 
				
			

This creates a 3×4 array with the values 1 to 12.

To access an element of a two-dimensional array, you need to use two indices: one for the row and one for the column. For example, to access the element in the second row and third column of the numbers array declared above, you would use:

				
					int element = numbers[1][2];   // element is 7 
				
			

In this example, the first index 1 specifies the row, and the second index 2 specifies the column.

You can declare and initialize multidimensional arrays with more than two dimensions by adding additional sets of square brackets for each additional dimension. For example, to declare a three-dimensional array with dimensions 3x4x2, you would use:

				
					datatype arrayname[3][4][2]; 
				
			

In summary, a multidimensional array in C is an array with more than one dimension. You can declare and initialize a multidimensional array using nested square brackets and curly braces, and access individual elements using multiple indices for each dimension.

Join To Get Our Newsletter
Spread the love