Array in C
Array in C
Creating an Array in C
The whole process of creating an array in C language can be divided into two primary sub
processes i.e.
1. Array Declaration
Array declaration is the process of specifying the type, name, and size of the array. In C, we
have to declare the array like any other variable before using it.
data_type array_name[size];
The above statements create an array with the name array_name, and it can store a
specified number of elements of the same data type.
Example:
// Creates array arr to store 5 integer values.
int arr[5];
When we declare an array in C, the compiler allocates the memory block of the specified
size to the array name.
Linkedin@KeerthanaGaliveeti
2. Array Initialization
Initialization in C is the process to assign some initial value to the variable. When the array
is declared or allocated memory, the elements of the array contain some garbage value. So,
we need to initialize the array to some meaningful values.
Syntax:
int arr[5] = {2, 4, 8, 12, 16};
The above statement creates an array arr and assigns the values {2, 4, 8, 12, 16} at the time
of declaration.
We can skip mentioning the size of the array if declaration and initialisation are done at the
same time. This will create an array of size n where n is the number of elements defined
during array initialisation. We can also partially initialize the array. In this case, the
remaining elements will be assigned the value 0 (or equivalent according to the type).
Linkedin@KeerthanaGaliveeti
//Partial Initialisation
int arr[5] = {2, 4, 8};
1/3
Example:
#include <stdio.h>
int main() {
Output
8 16 2
Linkedin@KeerthanaGaliveeti
Update Array Element
We can update the value of array elements at the given index i in a similar way to accessing
an element by using the array square brackets [] and assignment operator (=).
array_name[i] = new_value;
Example:
#include <stdio.h>
int main() {
int arr[5] = {2, 4, 8, 12, 16};
Output
1
C Array Traversal
Array Traversal is the process in which we visit every element of the array in a specific
order. For C array traversal, we use loops to iterate through each element of the array.
Traversing
An Array
Example:
Linkedin@KeerthanaGaliveeti
#include <stdio.h>
int main() {
int arr[5] = {2, 4, 8, 12, 16};
Output
2 4 8 12 16
Size of Array
The size of the array refers to the number of elements that can be stored in the array. The
array does not contain the information about its size but we can extract the size using
sizeof() operator.
Example:
#include <stdio.h>
int main() {
int arr[5] = {2, 4, 8, 12, 16};
#include <stdio.h>
int main() {
Output
0x7ffde73e54b0
0x7ffde73e54b0
To know more about the relationship between an array and a pointer, refer to this article
- Pointer to an Arrays | Array Pointer.
Passing Array to Function
In C, arrays are passed to functions using pointers, as the array name decays to a pointer to
the first element. So, we also need to pass the size of the array to the function.
Example:
#include <stdio.h>S
int main() {
int arr[] = {2, 4, 8, 12, 16};
int n = sizeof(arr) / sizeof(arr[0]);
Linkedin@KeerthanaGaliveeti
// Passing array
printArray(arr, n);
return 0;
}
Output
2 4 8 12 16
int arr[5]={1,2,4,8,7};
so
sizeof(arr)=20
and
sizeof(arr[0])=4
n=20/4
n=5
1. What is an array in C?
✅ Answer:
An array in C is a collection of fixed-size elements of the same data type, stored in contiguous memory
locations.
It allows storing multiple values under one name and accessing them using indexing.
Example:
c
CopyEdit
int marks[5] = {10, 20, 30, 40, 50};
📌 Internally:
If base address is 1000 (and size of int = 4 bytes), then:
Linkedin@KeerthanaGaliveeti
marks[0] → 1000
marks[1] → 1004
marks[2] → 1008 ... and so on
✅ Answer:
c
CopyEdit
int arr[5];
c
CopyEdit
int arr[] = {1, 2, 3, 4, 5}; // compiler infers size = 5
💡 Tip: Always ensure the size is a constant integer (except in C99+ where VLAs are allowed).
✅ Answer:
In C, arrays are zero-indexed, meaning the first element starts at index 0.
a[0] = 10
a[1] = 20
a[2] = 30
✅ Answer:
No. In C, once you declare an array with a fixed size, you cannot change it at runtime.
c
CopyEdit
int arr[5]; // size is fixed at compile-time
If you need dynamic sizing, use dynamic memory allocation with malloc() or use Variable Length Arrays
(C99+):
c
Linkedin@KeerthanaGaliveeti
CopyEdit
int n;
scanf("%d", &n);
int arr[n]; // VLA - Valid in C99 or newer
✅ Answer:
c
CopyEdit
int arr[5] = {1, 2, 3, 4, 5};
c
CopyEdit
int arr[5] = {1, 2}; // remaining elements = 0
c
CopyEdit
int arr[] = {1, 2, 3}; // compiler assigns size = 3
✅ Answer:
Accessing beyond the defined size of an array leads to undefined behavior.
The program might:
Example:
c
CopyEdit
int arr[3] = {1, 2, 3};
printf("%d", arr[5]); // Undefined behavior
👉 Compiler might not give an error, but it’s a logic error and dangerous.
✅ Answer:
Output: 0
Explanation:
arr[0] = 1
arr[1] = 2
arr[2], arr[3], arr[4] are auto-initialized to 0
✅ Answer:
Yes, in C99 and newer, you can declare Variable Length Arrays (VLAs).
c
CopyEdit
int n;
scanf("%d", &n);
int arr[n]; // valid in C99+
However, older C versions (C90) do not support VLAs. For dynamic sizes, use malloc():
c
CopyEdit
int *arr = (int *)malloc(n * sizeof(int));
✅ Answer:
c
CopyEdit
int arr[5] = {1, 2, 3, 4, 5};
printf("%d", arr[4]); // last element
c
CopyEdit
printf("%d", arr[sizeof(arr)/sizeof(arr[0]) - 1]);
c
CopyEdit
int arr[5];
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
This is a standard input method. Ensure the loop doesn’t exceed the declared size of the array.
c
CopyEdit
void inputArray(int arr[], int size) {
for (int i = 0; i < size; i++)
scanf("%d", &arr[i]);
}
For example, if you want to store 5 integers like 10, 20, 30, 40, 50, instead of declaring 5 separate
variables, you can use:
```c
int marks[5] = {10, 20, 30, 40, 50};
```
This simplifies both storage and access. Internally, the array elements are laid out in consecutive
memory addresses. If `marks[0]` is at memory address 1000 and `int` takes 4 bytes, then `marks[1]` is at
1004, `marks[2]` at 1008, and so on.
```c
Linkedin@KeerthanaGaliveeti
int arr[5];
```
This creates space for 5 integers, each of 4 bytes (typically), totaling 20 bytes. You can also initialize
during declaration:
```c
int arr[] = {1, 2, 3, 4, 5};
```
The compiler will count the elements and set the array size automatically.
```c
int arr[3] = {10, 20, 30};
printf("%d", arr[0]); // prints 10
```
This indexing scheme simplifies pointer arithmetic and is consistent with most C operations.
```c
int *arr = malloc(n * sizeof(int));
```
This allows flexibility, but you must manage memory manually using `free()`.
```c
int arr[5] = {1, 2, 3, 4, 5}; // full initialization
int arr[5] = {1, 2}; // rest become 0
int arr[] = {1, 2, 3}; // compiler sets size = 3
```
Uninitialized local arrays contain garbage values; global or static arrays are zero-initialized by default.
```c
int arr[3] = {1, 2, 3};
printf("%d", arr[5]); // undefined behavior
```
```c
int arr[5] = {1, 2};
printf("%d", arr[3]);
```
Linkedin@KeerthanaGaliveeti
In local scope, this may return a garbage value since only `arr[0]` and `arr[1]` are initialized. In
global/static scope, the remaining elements default to 0.
```c
int n;
scanf("%d", &n);
int arr[n];
```
This is not allowed in C90. For portability or dynamic needs, use `malloc()`:
```c
int *arr = malloc(n * sizeof(int));
```
```c
arr[4];
```
or more generally:
```c
arr[size - 1];
```
Linkedin@KeerthanaGaliveeti
```c
int size = sizeof(arr) / sizeof(arr[0]);
```
```c
int arr[5];
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
```
This is the standard way to read values into an array. The loop ensures each index receives input.
```c
int arr[10];
```
`sizeof(arr)` gives total size in bytes, and `sizeof(arr[0])` gives size of one element. This only works inside
For dynamically allocated arrays, you must track the size separately since `sizeof` won't work.
Linkedin@KeerthanaGaliveeti
**12. Can arrays be passed to functions? How?**
```c
```
Inside the function, `arr` behaves like a pointer. It does not carry size info, so size must be passed
separately.
**13. What is the difference between arr[] and *arr in function parameters?**
```c
```
Inside the function, `arr[i]` is interpreted as `*(arr + i)`. The difference is mostly syntactic.
```c
Linkedin@KeerthanaGaliveeti
int* createArray(int size) {
return arr;
}```
Linkedin@KeerthanaGaliveeti
**15. How are arrays stored in memory?**
```c
```
- `arr[1]` is at 1004
- `arr[2]` is at 1008
```c
arr[size - 1 - i] = temp;
```
```c
Linkedin@KeerthanaGaliveeti
int arr[10];
```
Linkedin@KeerthanaGaliveeti
- **Dynamic arrays**: Size decided at runtime using `malloc()` or `calloc()`.
```c
```
Static arrays are easier but less flexible. Dynamic arrays are flexible but need manual memory
management.
Use a loop:
```c
dest[i] = src[i];
```
Or use `memcpy`:
```c
```
```c
Linkedin@KeerthanaGaliveeti
This performs a quicksort based on your comparison logic.
Declaring an array parameter as `const` ensures it can't be modified inside the function:
```c
```
This is good practice when the function is only meant to read the array.
```c
int matrix[3][4];
```
```c
matrix[0][2];
```
```c
Linkedin@KeerthanaGaliveeti
```
Number of columns must be specified so the compiler knows the layout. Alternatively:
```c
```
**23. What is the difference between array of pointers and pointer to an array?**
- Array of pointers:
```c
int *arr[5];
```
- Pointer to array:
```c
int (*ptr)[5];
```
```c
int *p = arr;
```
Linkedin@KeerthanaGaliveeti
This is equivalent to `arr[2]`. Arrays and pointers are closely related.
Linkedin@KeerthanaGaliveeti
When passed to a function, arrays decay to pointers:
```c
```
```c
```
C doesn't support jagged arrays natively (arrays with rows of different lengths). But you can simulate
```c
int *arr[3];
```
Linkedin@KeerthanaGaliveeti
- Total size: `sizeof(arr)`
Linkedin@KeerthanaGaliveeti
- Number of elements: `sizeof(arr) /
```c
int sum = 0;
for (int i = 0; i
sum +=
arr[i];
```
```c
```
```c
Linkedin@KeerthanaGaliveeti
i++) {
if (arr[i] >
max) max =
arr[i]; if
min = arr[i];
Linkedin@KeerthanaGaliveeti