0% found this document useful (0 votes)
4 views4 pages

C Arrays and Strings Explained

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)
4 views4 pages

C Arrays and Strings Explained

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

### Define Array and Its Characteristics

An **array** in C is a collection of elements of the same data type stored in contiguous memory

locations.

It allows you to store multiple values in a single variable, accessed using an index.

#### Characteristics of Arrays

1. **Homogeneous Elements**: All elements in an array are of the same data type.

2. **Contiguous Memory Allocation**: Elements are stored in adjacent memory locations.

3. **Fixed Size**: The size of an array must be specified during its declaration and cannot be

changed.

4. **Index-Based Access**: Elements are accessed using zero-based indexing.

5. **Efficient Traversal**: Arrays provide a straightforward mechanism to traverse elements.

6. **Random Access**: Directly access any element using its index.

---

### Nested Structures

Nested structures are structures that contain one or more structures as members. They allow

hierarchical organization of data, representing real-world entities with multiple attributes.

#### Example

```c

#include <stdio.h>

struct Address {

char city[50];
int pincode;

};

struct Person {

char name[50];

int age;

struct Address address;

};

int main() {

struct Person person = {"John", 30, {"New York", 12345}};

printf("Name: %s, Age: %d, City: %s, Pincode: %d\n",

[Link], [Link], [Link], [Link]);

return 0;

```

---

### Multidimensional Arrays

Multidimensional arrays are arrays of arrays. The most common is the **two-dimensional array**,

which can be visualized as a table with rows and columns.

#### Syntax:

```c

data_type array_name[size1][size2];

```
#### 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;

```

---

### Strings in C

Strings in C are arrays of characters, terminated by a null character (`\0`). The `string.h` library

provides functions to manipulate strings.

#### Commonly Used String Library Functions

- `strlen()`: Returns string length.

- `strcpy()`: Copies a string.

- `strcat()`: Concatenates strings.

- `strcmp()`: Compares two strings.


- `strchr()`: Finds a character in a string.

- `strstr()`: Finds a substring in a string.

#### Example of `strlen()`:

```c

#include <stdio.h>

#include <string.h>

int main() {

char str[] = "Hello, World!";

printf("Length of string: %zu\n", strlen(str));

return 0;

```

Refer to the previous sections for a detailed explanation of arrays and the string library.

You might also like