Arrays in C
Array declaration
In C, an array is a collection of elements of the same data type, accessed using a common name and an index or subscript to identify a specific element. Arrays in C are defined by specifying the data type of the elements and the number of elements in the array. Here is an example of defining an integer array with 5 elements in C:
int myArray[5];
This creates an integer array called 'myArray' with 5 elements. The elements can be accessed using their index starting at 0, like this:
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;
myArray[3] = 4;
myArray[4] = 5;
Arrays can also be initialized at the time of declaration like this:
int myArray[] = {1, 2, 3, 4, 5};
In addition to one-dimensional arrays, C also supports multi-dimensional arrays. For example, a two-dimensional array can be defined like this:
int myArray[3][4];
This creates a two-dimensional array with 3 rows and 4 columns. The elements can be accessed using two indices, like this:
myArray[0][0] = 1;
myArray[0][1] = 2;
myArray[0][2] = 3;
myArray[0][3] = 4;
myArray[1][0] = 5;
myArray[1][1] = 6;
// and so on
Arrays in C can be very useful for storing and manipulating data in a program. However, care must be taken to avoid out-of-bounds access, which can lead to undefined behavior or crashes.
Passing 1D and 2D arrays to a function
1D array to a function
To pass a one-dimensional (1D) array to a function in C, we can declare the function argument as a pointer to the array type. Here's an example:void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int myArray[] = {1, 2, 3, 4, 5};
int size = sizeof(myArray) / sizeof(myArray[0]);
printArray(myArray, size);
return 0;
}
In this example, the 'printArray' function takes two arguments: a pointer to an integer array 'arr' and an integer 'size' indicating the number of elements in the array. The function simply prints out each element of the array using a loop.2D array to a function
To pass a two-dimensional (2D) array to a function in C, we can declare the function argument as a pointer to an array of arrays, where the second dimension of the array is specified. Here's an example:void printMatrix(int matrix[][3], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int myMatrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
printMatrix(myMatrix, 2, 3);
return 0;
}
In this example, the 'printMatrix' function takes three arguments: a pointer to a 2D integer array 'matrix', an integer 'rows' indicating the number of rows in the array, and an integer 'cols' indicating the number of columns in the array. The function prints out each element of the 2D array using nested loops.Array operations
Updating array elements:Individual elements of an array can be updated by assigning new values to them. For example:
Copying arrays:
int myArray[5] = {1, 2, 3, 4, 5};
myArray[2] = 10;
printf("%d\n", myArray[2]); // Output: 10
Arrays can be copied using loops:
Insertion:
int myArray1[5] = {1, 2, 3, 4, 5};
int myArray2[5];
for (int i = 0; i < 5; i++) {
myArray2[i] = myArray1[i];
}//myArray2 = {1,2,3,4,5}
To insert an element in an array, we can shift the elements to the right of the insertion point and then assign the new value to the desired index. Here's an example:
int arr[] = {1,2,3,4,5,6,7,8,9};
int size = sizeof(arr)/sizeof(arr[0]);
int value = 10,index = 3;
for (int i = size - 1; i >= index ; i--)
{
arr[i+1] = arr[i];
}
arr[index] = value;
size++;
for (int i = 0; i < size; i++)
{
printf("%d ",arr[i]);
}// output 1 2 3 10 4 5 6 7 8 9
This inserts the value 10 at index 2 in the array 'arr'. Here is a another example with using functions:
Deletion:
void insertion(int *arr,int *size,int value,int index){
for (int i = *size - 1 ; i >= index; i--)
{
arr[i+1] = arr[i];
}
arr[index] = value;
*size = *size + 1;
}
void main(){
int arr[] = {1,2,3,4,5,6,7,8,9};
int size = sizeof(arr)/sizeof(arr[0]);
int val = 10,index = 4;
insertion(arr,&size,val,index);
for (int i = 0; i < size; i++)
{
printf("%d ",arr[i]);
}// output 1 2 3 4 10 5 6 7 8 9
}To delete an element from an array, we can shift the elements to the left of the deletion point and decrease the size of the array. Here's an example:
int arr[] = {1,2,3,4,5,6,7,8,9};
int size = sizeof(arr)/sizeof(arr[0]);
int index = 3;
for (int i = index; i < size ; i++)
{
arr[i] = arr[i+1];
}
size--;
for (int i = 0; i < size; i++)
{
printf("%d ",arr[i]);
}// output 1 2 3 5 6 7 8 9
This deletes the element at index 3 in the array "arr". Here is a another example with using functions:
void deletion(int *arr,int *size,int index){
for (int i = index; i < *size; i++)
{
arr[i] = arr[i+1];
}
*size = *size - 1;
}
void main(){
int arr[] = {1,2,3,4,5,6,7,8,9};
int size = sizeof(arr)/sizeof(arr[0]);
int val = 10,index = 4;
deletion(arr,&size,index);
for (int i = 0; i < size; i++)
{
printf("%d ",arr[i]);
}// output 1 2 3 4 6 7 8 9
}
Comments
Post a Comment