Back to C Programming
2026-07-146 min read

C - Properties of Array

Learn C - Properties of Array step by step with clear examples and exercises.

Why This Matters

Understanding the properties of arrays is crucial for efficient and effective coding in C. Arrays are fundamental data structures used to store multiple values of the same type in contiguous memory locations. Mastering array properties will help you tackle complex algorithms, manage large datasets, and excel in exams, interviews, and real-world software development.

Prerequisites

Before delving into C's array properties, it is essential to have a strong grasp of the following concepts:

  1. Basic C syntax, including variables, operators, control structures (if statements, loops), and data types (int, char, float, etc.)
  2. Memory management in C, including pointer arithmetic and dynamic memory allocation
  3. Functions and function calls, including pointers and references
  4. Understanding of C standard library functions such as printf, scanf, and others

Core Concept

Definition and Declaration

An array is a collection of elements of the same data type stored in contiguous memory locations. The first element's index is 0, and the last one's index is one less than the array size. To declare an array, specify its name, data type, and size enclosed in square brackets:

int arr[5]; // Declare an array of 5 integers
char str[10]; // Declare a character array (string) of length 10

Initialization

You can initialize an array by assigning values to its elements during declaration:

int arr[5] = {1, 2, 3, 4, 5}; // Initialize an array with given values
char str[10] = "Hello, World!"; // Initialize a string with a constant character sequence

Accessing Elements

To access an element of an array, use its name followed by the index enclosed in square brackets:

int arr[5] = {1, 2, 3, 4, 5};
printf("%d", arr[0]); // Output: 1

Array Size

The size of an array is determined at compile time and cannot be changed during runtime. To find the size of an array, use the sizeof operator:

int arr[5];
printf("%d", sizeof(arr)); // Output: 20 (assuming int takes 4 bytes)

Multidimensional Arrays

Multidimensional arrays are used to store multiple sets of contiguous memory locations. They can be declared as follows:

int arr[3][4]; // Declare a 3x4 2D array of integers

Passing Arrays to Functions

To pass an array to a function, use its name without the square brackets:

void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}

int main() {
int arr[5] = {1, 2, 3, 4, 5};
printArray(arr, sizeof(arr) / sizeof(arr[0]));
return 0;
}

Array Pointers

Arrays in C decay into pointers when passed to functions or assigned to pointers. This means that you can treat an array as a pointer and manipulate it using pointer arithmetic:

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // ptr now points to the first element of arr
printf("%d", *ptr); // Output: 1

Dynamic Memory Allocation

Dynamic memory allocation allows you to create arrays of variable size during runtime using functions like malloc, calloc, and realloc. This can be useful when the array size is not known at compile time or when dealing with large datasets.

Worked Example

Let's implement a program that sorts an array of integers using the bubble sort algorithm:

#include <stdio.h>

void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}

void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}

int main() {
int arrSize;
printf("Enter the size of the array: ");
scanf("%d", &arrSize);

int* arr = (int*)malloc(arrSize * sizeof(int)); // Dynamically allocate memory for the array

printf("Enter %d integers:", arrSize);
for (int i = 0; i < arrSize; i++) {
scanf("%d", &arr[i]);
}

bubbleSort(arr, arrSize);

printf("Sorted array: ");
for (int i = 0; i < arrSize; i++) {
printf("%d ", arr[i]);
}

free(arr); // Free the dynamically allocated memory
return 0;
}

Common Mistakes

  1. Forgetting to initialize an array: This can lead to undefined behavior and hard-to-debug issues.
  2. Accessing out-of-bounds elements: Always ensure that the index is within the valid range (0 to size - 1).
  3. Not passing the correct size of the array to functions: Pass the size as a separate argument or use the sizeof operator to calculate it inside the function.
  4. Using pointer arithmetic incorrectly with arrays: Be careful when using pointers with arrays, as they are essentially just pointers themselves.
  5. Not understanding multidimensional arrays: Remember that elements in a multidimensional array are accessed using multiple indices (e.g., arr[i][j] for a 2D array).
  6. Forgetting to deallocate dynamically allocated memory: Always free the memory when it is no longer needed to avoid memory leaks.
  7. Not handling edge cases in algorithms: Ensure that your algorithms work correctly for arrays of zero or one element and handle any other special cases as necessary.

Practice Questions

  1. Write a function to find the maximum element in an array of integers.
  2. Implement a program that sorts an array of strings lexicographically using a comparison function.
  3. Write a function that reverses the order of elements in an array.
  4. Create a program that finds the second-largest number in an array of integers (assuming there are no duplicates).
  5. Implement a 3x3 matrix multiplication function using multidimensional arrays.
  6. Write a function to find the sum of all odd numbers in an array of integers.
  7. Implement a program that finds the first missing positive integer in an unsorted array (assuming there are no duplicates and the smallest number is 1).
  8. Create a function that concatenates two strings using dynamic memory allocation.
  9. Write a function to find the index of the kth smallest element in an array of integers.
  10. Implement a program that finds all pairs of integers in an array whose sum equals a given target value.

FAQ

  1. Why can't I change the size of an array during runtime? Changing the size of an array would require reallocating memory, which is not supported in C without dynamic memory allocation (using malloc and friends).
  2. What happens if I try to access an out-of-bounds element in an array? Accessing an out-of-bounds element can lead to undefined behavior, such as segmentation faults or incorrect results.
  3. Can I use a 2D array to store different data types? No, each element of a 2D (or higher-dimensional) array must have the same data type. However, you can use a struct to create a 2D array with different data types.
  4. How do I declare and initialize a 2D array with given values? To declare a 2D array with given values, use braces to specify the initial values for each row:
int arr[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
  1. What is the difference between a pointer and an array in C? In C, arrays decay into pointers when passed to functions or assigned to pointers. However, arrays have a fixed size at compile time, while pointers can point to any memory location.
  2. How do I find the number of elements in an array without using sizeof? You can iterate through the array and count the number of non-zero elements:
int arr[5] = {1, 0, 3, 0, 5};
int size = 0;
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {
if (arr[i]) {
size++;
}
}