0% found this document useful (0 votes)
5 views

arrays_in_c

Uploaded by

jeefcker9647
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

arrays_in_c

Uploaded by

jeefcker9647
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Notes on Arrays in C Programming

1. Array Declaration:

An array is declared with a fixed size, defining the number of elements.

Syntax: data_type array_name[array_size];

2. Array Initialization:

Arrays can be initialized at the time of declaration or assigned later.

Example: int arr[5] = {1, 2, 3, 4, 5};

3. Accessing Elements:

Array elements are accessed using their index, starting from 0.

Example: printf("%d", arr[0]); // Outputs the first element.

4. Multidimensional Arrays:

C supports multidimensional arrays for complex data structures.

Example: int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

5. Key Operations:

- Accessing elements: Use array[index].

- Modifying elements: Assign values to specific indices.

- Iterating through arrays: Use loops to read/modify elements.

6. Important Notes:

- Size of the array is fixed at the time of declaration.

- No automatic bounds checking, leading to potential runtime errors.


7. Advantages of Arrays:

- Efficient for storing and accessing multiple values of the same type.

- Easy to iterate and manipulate using loops.

8. Limitations of Arrays:

- Fixed size (static allocation) in standard C.

- No automatic bounds checking, risking out-of-bounds access.

Code Examples:

1. One-Dimensional Array Example:

```c

#include <stdio.h>

int main() {

int arr[5] = {10, 20, 30, 40, 50}; // Declare and initialize array

for(int i = 0; i < 5; i++) {

printf("Element at index %d is: %d

", i, arr[i]);

return 0;

```
2. Two-Dimensional Array Example:

```c

#include <stdio.h>

int main() {

int matrix[3][3] = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};

for(int i = 0; i < 3; i++) {

for(int j = 0; j < 3; j++) {

printf("%d ", matrix[i][j]);

printf("\n");

return 0;

```

You might also like