Skip to content

Commit 6efdb54

Browse files
authored
Update Array.md
1 parent 3b14daa commit 6efdb54

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Array.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,31 @@ Here, we declared an array, a, of integer type. And its size is 5. Meaning, it c
2727
It's important to note that the size and type of an array cannot be changed once it is declared.
2828

2929
## How to initialize an array?
30+
It is possible to initialize an array during declaration. For example,
31+
```c
32+
int a[5] = { 23, 54, 67, 21, 88};
33+
```
34+
35+
You can also initialize an array like this.
36+
37+
```c
38+
int a[] = {12, 54, 67, 23, 32}
39+
```
40+
Here, I haven't specified the size. However, the compiler knows its size is 5 as we are initializing it with 5 elements.
41+
42+
## Memory allocation of Array
43+
All the data elements of an array are stored at contiguous locations in the main memory. The name of the array represents the base address or the address of first element in the main memory. Each element of the array is represented by a proper indexing.
44+
45+
## Properties of array
46+
* Each element is of same data type and carries a same size i.e. int = 4 bytes.
47+
* Elements of the array are stored at contiguous memory locations where the first element is stored at the smallest memory location.
48+
* Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of data element.
49+
50+
## How to access elements of Array
51+
You can access elements with the help of the index at which you stored them.
52+
53+
Suppose you declared an array a as above. The first element is a[0], the second element is a[1] and so on.
54+
55+
```c
56+
printf(“%d\n”,a[0]);
57+
```

0 commit comments

Comments
 (0)