Back to C Programming
2025-11-255 min read

22.1.4.3 Type qualifiers on array parameters (C Programming)

Learn 22.1.4.3 Type qualifiers on array parameters (C Programming) step by step with clear examples and exercises.

Title: Type Qualifiers on Array Parameters in C Programming

Why This Matters

Type qualifiers are crucial tools for C programmers, enabling efficient data manipulation and memory management. In this lesson, we delve into type qualifiers used with array parameters, which can lead to more effective and readable code. Understanding these qualifiers is essential for mastering programming interviews, debugging real-world bugs, and writing cleaner, maintainable code.

Prerequisites

To fully grasp the concepts presented in this lesson, you should have a strong understanding of:

  • C data types (int, char, float, etc.)
  • Arrays in C
  • Pointers in C
  • Passing arguments by value and reference
  • Basic memory allocation and deallocation
  • The concept of constant and volatile variables

Core Concept

In C programming, arrays are passed to functions by their addresses. However, you can use type qualifiers to modify the behavior of array parameters:

  1. const: Declaring an array as const means that its contents cannot be modified within the function. The syntax is as follows:
void myFunction(const int arr[5]);
  1. volatile: This qualifier indicates that the memory location of the variable may be modified by hardware independently of the C program. It's used when working with external devices or interrupt handlers. The syntax is as follows:
void myFunction(volatile int arr[5]);

const vs non-const arrays

When you pass a non-constant array to a function, the function can modify its contents. However, when you use the const qualifier, the function can still read the array's contents but cannot modify it:

void myFunction(int arr[5]) {
arr[0] = 42; // This will compile without errors, but the value of arr[0] remains unchanged in the calling scope.
}

void myConstFunction(const int arr[5]) {
arr[0] = 42; // This will result in a compilation error because the array is declared as const.
}

Worked Example

Let's explore an example where we have a function that sorts an array of integers using the bubble sort algorithm, ensuring that the array remains constant within the function:

#include <stdio.h>

void bubbleSort(const int arr[], int n) {
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int arr[] = {3, 5, 2, 7, 1};
int n = sizeof(arr) / sizeof(arr[0]);

bubbleSort(arr, n);

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

return 0;
}

In this example, the bubbleSort function receives a constant array as an argument. The const qualifier ensures that the array's contents cannot be modified within the function, allowing us to sort it safely without worrying about unintended side effects.

Common Mistakes

  1. Forgetting to use const when passing an array to a function:
void myFunction(int arr[5]); // Incorrect; the array is modifiable within the function
  1. Using volatile incorrectly or unnecessarily:
void myFunction(volatile int arr[5]) { /* This may not be necessary if you're not working with external devices or interrupt handlers */ }
  1. Not understanding the difference between const and non-const arrays:
// Incorrect use of const when passing an array to a function that modifies its contents
void myFunction(const int arr[5]) {
arr[0] = 42; // This will result in a compilation error because the array is declared as const.
}

Common Mistakes (continued)

  1. Modifying the size of a constant array within a function:
void myFunction(const int arr[5]) {
// Incorrect; you cannot change the size of a constant array within a function.
arr[5] = 99;
}

Practice Questions

  1. Write a function that takes a constant array of integers as an argument and returns the sum of its elements.
int sumArray(const int arr[], int n) {
int total = 0;
for (int i = 0; i < n; ++i) {
total += arr[i];
}
return total;
}
  1. Modify the bubble sort example to handle arrays of different sizes.
void bubbleSort(const int arr[], int n) {
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int arr[] = {3, 5, 2, 7, 1};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);

// Get the size of a user-defined array and sort it:
int size;
printf("Enter the size of your array: ");
scanf("%d", &size);
int userArr[size];

for (int i = 0; i < size; ++i) {
printf("Enter element %d: ", i + 1);
scanf("%d", &userArr[i]);
}

bubbleSort(userArr, size);

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

return 0;
}
  1. Implement a function that reverses an array using pointers and the const qualifier.
void reverseArray(const int arr[], int n) {
for (int i = 0, j = n - 1; i < n / 2; ++i, --j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

FAQ

What happens if I pass a non-constant array to a function with the const qualifier?

  • The function can still read the array's contents but cannot modify it.

When should I use the volatile qualifier when working with arrays in C?

  • Use volatile when working with external devices or interrupt handlers to ensure that the memory location of the variable is updated correctly.

Can I declare a constant array and modify its size within a function?

  • No, you cannot change the size of a constant array within a function. If you need to work with arrays of varying sizes, consider using dynamic memory allocation or passing the array length as an additional argument to your functions.

What is the difference between const and non-const arrays when passed to a function?

  • A non-constant array can be modified within the function, while a constant array cannot be modified. This means that if you pass a constant array to a function, the function can still read its contents but cannot change them.