The Wayback Machine - https://web.archive.org/web/20241005095335/https://www.geeksforgeeks.org/properties-of-array-in-c/
Open In App

Properties of Array in C

Last Updated : 23 Aug, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

An array in C is a fixed-size homogeneous collection of elements stored at a contiguous memory location. It is a derived data type in C that can store elements of different data types such as int, char, struct, etc. It is one of the most popular data types widely used by programmers to solve different problems not only in C but also in other languages.

The properties of the arrays depend on the programming language. In this article, we will study the different properties of Array in the C programming language.

  1. Fixed Size Collection
  2. Homogeneous Elements
  3. Indexing in Array
  4. Dimensions of Array
  5. Contiguous Storage
  6. Random Access
  7. Array name relation with pointer
  8. Bound Checking
  9. Array Decay

C Array Properties

1. Fixed Size of an Array

In C, the size of an array is fixed after its declaration. It should be known at the compile time and it cannot be modified later in the program. The below example demonstrates the fixed-size property of the array.

Example:

C




// C Program to Illustrate the Fixed Size Properties of the
// Array
#include <stdio.h>
 
int main()
{
 
    // creating a new array of size 5
    int array[5] = { 1, 2, 3, 4, 5 };
 
    printf("Size of Array Before: %d\n",
           sizeof(array) / sizeof(int));
 
    // trying to increase the size of the array
    array[6];
    // not checking the size
    printf("Size of Array After: %d",
           sizeof(array) / sizeof(int));
 
    return 0;
}


Output

Size of Array Before: 5
Size of Array After: 5

2. Homogeneous Collection

An array in C cannot have elements of different data types. All the elements are of the same type.

Example:

C




// C program to Demonstrate the Homogeneous Property of the
// C Array
#include <stdio.h>
 
int main()
{
 
    // declaring integer array
    int arr[3] = { 1, 2 };
 
    // trying to store string in the third element
    arr[2] = "Geeks";
 
  // printing elements
    printf("Array[1]: %d\n", arr[0]);
    printf("Array[2]: %d\n", arr[1]);
    printf("Array[3]: %s", arr[2]);
 
    return 0;
}


Output

main.c: In function ‘main’:
main.c:12:16: warning: assignment to ‘int’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
   12 |         arr[2] = "Geeks";
      |                ^
main.c:17:28: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
   17 |         printf("Array[3]: %s", arr[2]);
      |                           ~^   ~~~~~~
      |                            |      |
      |                            char * int
      |                           %d
Array[1]: 1
Array[2]: 2

3. Indexing in an Array

Indexing of elements in an Array in C starts with 0 instead of 1. It means that the index of the first element will be 0 and the last element will be (size – 1) where size is the size of the array.

indexing of array elements in c

Indexing of Elements in C Array

Example:

C




// C Program to Illustrate Array Indexing in C
#include <stdio.h>
 
int main()
{
 
    // creating integer array with 2 elements
    int arr[2] = { 10, 20 };
 
    // printing element at index 1
    printf("Array[1]: %d\n", arr[1]);
 
    // printing element at index 0
    printf("Array[0]: %d", arr[0]);
   
    return 0;
}


Output

Array[1]: 20
Array[0]: 10

As we see in the above example, at index 1, the second element is present while at index 0, the first element is present.

4. Dimensions of the Array

An array in C can be a single dimensional like a 1-D array or multidimensional like a 2-D array, 3-D array, and so on. It can have any number of dimensions. The number of elements in a multidimensional array is the product of the size of all the dimensions.

arrays of different dimensions

Arrays of Different Dimensions

Example:

C




// C Program to create multidimensional array
#include <stdio.h>
 
int main()
{
 
    // creating 2d array
    int arr2d[2][2] = { 1, 2, 3, 4 };
 
    // creating 3d array
    int arr3d[2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8 };
 
    printf("2D Array: ");
    // printing 2d array
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            printf("%d ", arr2d[i][j]);
        }
    }
 
    printf("\n3D Array: ");
    // printing 3d array
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                printf("%d ", arr3d[i][j][k]);
            }
        }
    }
 
    return 0;
}


Output

2D Array: 1 2 3 4 
3D Array: 1 2 3 4 5 6 7 8 

5. Contiguous Storage

All the elements in an array are stored at contiguous or consecutive memory locations. We can easily imagine this concept in the case of a 1-D array but multidimensional arrays are also stored contiguously. It is possible by storing them in row-major or column-major order where the row after row or column after the column is stored in the memory. We can verify this property by using pointers.

Example:

C++




// C Program to Verify the Contiguous Storage of Elements in
// an Array
#include <stdio.h>
 
int main()
{
 
    // creating an array of 5 elements
    int arr[5] = { 1, 2, 3, 4, 5 };
 
    // defining pointers to 2 consecutive elements
    int* ptr1 = &arr;[1];
    int* ptr2 = &arr;[2];
 
    // printing the address of arr[1] and arr[2]
    printf("Address of arr[1] : %p\n", ptr1);
    printf("Address of arr[2] : %p", ptr2);
 
    return 0;
}


Output

Address of arr[1] : 0x7fffb8cc1ef4
Address of arr[2] : 0x7fffb8cc1ef8

In the above example, the difference between the addresses of arr[1] and arr[2] is 4 bytes which is the memory required to store a single integer. So, at memory addresses 0x7ffebc02e054 to 0x7ffebc02e057, arr[1] is stored and in the next 4 bytes, arr[2] is stored. The same is true for all the elements.

6. Random Access to the Elements

It is one of the defining properties of an Array in C. It means that we can randomly access any element in the array without touching any other element using its index. This property is the result of Contiguous Storage as a compiler deduces the address of the element at the given index by using the address of the first element and the index number.

Address of ith = Address of 1st Element + (Index * Size of Each Element)
array elements with memory addresses

Array in C

We can verify this by using Pointer Arithmetic.

Example:

C




// C Program to check the random access property of the
// array
#include <stdio.h>
 
int main()
{
 
    // creating an array of 5 elements
    int arr[5] = { 1, 2, 3, 4, 5 };
 
    // address of first element
    int* ptr = &arr;[0];
 
    // printing arr[3]
    printf("Array[3]: %d\n", arr[3]);
 
    // printing element at index 3 using ptr
    printf("Array[3] using pointer to first element = %d",
           *(ptr + 3));
 
    return 0;
}


Output

Array[3]: 4
Array[3] using pointer to first element = 4

Note: We have not multiplied the size of each element in the code as the compiler deduce and multiply it automatically by itself whenever we perform pointer arithmetic.

7. Relationship between Array and Pointers

Arrays are closely related to pointers in the sense that we can do almost all the operations possible on an array using pointers. The array’s name itself is the pointer to its first element.

Example:

C




// C Program to Illustrate the Relationship Between Array
// and Pointers
#include <stdio.h>
 
int main()
{
 
    // creating an array with 3 elements
    int arr[3] = { 1, 2, 3 };
 
    int* ptr = &arr;[0];
 
    // Pointer to first element
    printf("Pointer to First Element: %p\n", ptr);
 
    // Array name as pointer
    printf("Arran Name: %p", arr);
 
    return 0;
}


Output

Pointer to First Element: 0x7ffec5059660
Arran Name: 0x7ffec5059660

The relationship between pointer and array is very deep and we can study more about it in other articles such as – Pointer to an Array | Array Pointer

8. Bound Checking

Bound checking is the process in which it is checked whether the referenced element is present within the declared range of the Array. In C language, array bound checking is not performed so we can refer to the elements outside the declared range of the array leading to unexpected errors.

Example:

C




// C Program to Illustrate the Out of Bound access in arrays
#include <stdio.h>
 
int main()
{
 
    // creating new array with 3 elements
    int arr[3] = { 1, 2, 3 };
 
    // trying to access out of bound element
    printf("Some Garbage Value: %d", arr[5]);
 
    return 0;
}


Output

Some Garbage Value: 0

As seen in the above example, there is no error shown by the compiler while accessing memory that is out of array bounds.

9. Array Decay

Array decay is the process in which an array in C loses its dimension in certain conditions and decays into pointers. After this, we cannot determine the size of the array using sizeof() operator. It happens when an array is passed as a pointer.

Example:

C




// C Program to Demonstrate the Array Decay
#include <stdio.h>
 
// function
void func(int* arr)
{
    printf("Sizeof Value in Function: %d", sizeof(arr));
}
 
int main()
{
 
    // creating array with 3 elements
    char arr[3];
 
    printf("Sizeof Value in Main: %d\n", sizeof(arr));
 
    // passing array
    func(arr);
 
    return 0;
}


Output

Sizeof Value in Main: 3
Sizeof Value in Function: 8

The size of the array in the main() is 3 bytes which is the actual size of the array but when we check the size of the array in func(), the size comes out to be 8 bytes which instead of being the size of the array, it is the size of the pointer to the first element of the array.



Previous Article
Next Article

Similar Reads

What’s difference between “array” and “&array” for “int array[5]” ?
If someone has defined an array such as “int array[5]”, what’s the meaning of “array” or “&array”? Are they both same or are they different? You might be tempted to think that they both would point to the very first element of the array i.e. they both will have same address. Let us find out! To check this, the very first thing that comes to min
4 min read
Difference between pointer to an array and array of pointers
Pointer to an array: Pointer to an array is also known as array pointer. We are using the pointer to access the components of the array. int a[3] = {3, 4, 5 }; int *ptr = a; We have a pointer ptr that focuses to the 0th component of the array. We can likewise declare a pointer that can point to whole array rather than just a single component of the
4 min read
Jagged Array or Array of Arrays in C with Examples
Prerequisite: Arrays in C Jagged array is array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D array but with a variable number of columns in each row. These type of arrays are also known as Jagged arrays. Example: arr[][] = { {0, 1, 2}, {6, 4}, {1, 7, 6, 8, 9}, {5} }; Below are the methods to implement the j
3 min read
Array of Structures vs Array within a Structure in C
Both Array of Structures and Array within a Structure in C programming is a combination of arrays and structures but both are used to serve different purposes. Array within a Structure A structure is a data type in C that allows a group of related variables to be treated as a single unit instead of separate entities. A structure may contain element
5 min read
Pointer to an Array | Array Pointer
Prerequisite: Pointers Introduction  Consider the following program:  [GFGTABS] C #include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr; printf("%p\n", ptr); return 0; } [/GFGTABS]In the above program, we have a pointer ptr that points to the 0th element of the array. Similarly, we can also declare a pointer
9 min read
Pointer vs Array in C
Most of the time, pointer and array accesses can be treated as acting the same, the major exceptions being: 1. the sizeof operator sizeof(array) returns the amount of memory used by all elements in the array sizeof(pointer) only returns the amount of memory used by the pointer variable itself 2. the & operator array is an alias for &array[0
1 min read
Sum of array using pointer arithmetic
Given an array, write a program to find the sum of array using pointers arithmetic. In this program we make use of * operator . The * (asterisk) operator denotes the value of variable. The * operator at the time of declaration denotes that this is a pointer, otherwise it denotes the value of the memory location pointed by the pointer . sum() functi
2 min read
Sort an array using socket programming in C
Given an array of unsorted positive integer, sort the given array using the Socket programming . Examples: Input : 4 5 6 1 8 2 7 9 3 0 Output :0 1 2 3 4 5 6 7 8 9 Input : 9 8 1 4 0 Output : 0 1 4 8 9 Compile these files using gcc command (gcc client.c -o client and gcc server.c -o server). Run the program using ./server and ./client (Please note :
3 min read
4 Dimensional Array in C/C++
Prerequisite :Array in C/C++, More on array A four-dimensional (4D) array is an array of array of arrays of arrays or in other words 4D array is a array of 3D array. More dimensions in an array means more data be held, but also means greater difficulty in managing and understanding arrays. Declaration of a Multidimensional Array in C: Syntax: data_
3 min read
Input an integer array without spaces in C
How to input a large number (a number that cannot be stored even in long long int) without spaces? We need this large number in an integer array such that every array element stores a single digit. Input : 10000000000000000000000000000000000000000000000 We need to read it in an arr[] = {1, 0, 0...., 0} In C scanf(), we can specify count of digits t
2 min read
Different ways to Initialize all members of an array to the same value in C
An array is a collection of data that holds fixed number of values of same type. For example: if you want to store marks of 100 students, you can create an array for it. int num[100]; How to declare an array in C? Data_type array_name[size_of_array]; For example, float num[10]; Below are some of the different ways in which all elements of an array
3 min read
Difference between Structure and Array in C
Array in C An array is collection of items stored at contiguous memory locations. Structure in C A structure is a user defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. Difference between Structure and Array ARRAYSTRUCTUREArray refers to a collection consistin
2 min read
C program to Insert an element in an Array
Given an array arr of size n, this article tells how to insert an element x in this array arr at a specific position pos. An array is a collection of items stored at contiguous memory locations. In this article, we will see how to insert an element in an array in C. Follow the below steps to solve the problem: First get the element to be inserted,
2 min read
Count ways to reach the Nth stair using any step from the given array
Given N stairs and a person standing at the bottom wants to reach the top. He could climb any number of steps from the given array arr[] of positive integers. The task is to find the count of all possible ways to reach the top. Examples: Input: arr[] = {1, 3, 5}, N = 5 Output: 5 (0 -> 1 -> 2 -> 3 -> 4 -> 5), (0 -> 1 -> 2 ->
10 min read
Duplicates Removal in Array using BST
Given an array arr[] of integers, the task is to remove duplicates from the given array. Examples: Input: arr[] = {1, 2, 3, 2, 5, 4, 4} Output: arr[] = {1, 2, 3, 4, 5} Input: arr[] = {127, 234, 127, 654, 355, 789, 355, 355, 999, 654} Output: arr[] = {127, 234, 355, 654, 789, 999} The duplicates in the array can be removed using Binary Search Tree.
7 min read
Move all zeroes to end of array using Two-Pointers
Given an array of random numbers, Push all the zero’s of the given array to the end of the array. For example, if the given arrays is {1, 0, 2, 6, 0, 4}, it should be changed to {1, 2, 6, 4, 0, 0}. The order of all other elements should be the same.Examples: Input: arr[]={8, 9, 0, 1, 2, 0, 3} Output: arr[]={8, 9, 1, 2, 3, 0, 0} Explanation: Swap {0
6 min read
Reverse an array in groups of given size | Set 3 (Single traversal)
Given an array, reverse every sub-array formed by consecutive k elements. Examples: Input: arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3. Output: [3, 2, 1, 6, 5, 4, 9, 8, 7, 10]Input: arr = [1, 2, 3, 4, 5, 6, 7], k = 5. Output: [5, 4, 3, 2, 1, 7, 6] Approach: We will use two pointers technique to solve this problem.First, we will initialize our 1st
6 min read
Difference between Array and Union in C
1. Array in C : An array is collection of similar data items accessed by a common name stored at continuous memory locations. The elements of an array can be accessed using indices. They can be used to store primitive data types such as int, float, double, char, etc. but all elements have to be of the same data type. Below is the picturesque repres
2 min read
How to Get Value of Multidimensional Array in C?
Prerequisite: Array in C An array is a type of data structure where we can store multiple elements of similar data types. A multidimensional array can be termed an array of arrays that stores homogeneous data in tabular form. Data in multidimensional arrays are stored in row-major order. Declaration of Array Declaration or Initialisation of an arra
8 min read
How to Declare and Initialize an Array of Pointers to a Structure in C?
Prerequisite: Structure in CArray in C In C language, arrays are made to store similar types of data in contiguous memory locations. We can make arrays of either primitive data types, like int, char, or float, or user-defined data types like Structures and Unions. We can also make arrays of pointers in C language. Today we will learn how to create
8 min read
How does C allocate memory of data items in a multidimensional array?
The data items in a multidimensional array are stored in the form of rows and columns. Also, the memory allocated for the multidimensional array is contiguous. So the elements in multidimensional arrays can be stored in linear storage using two methods i.e., row-major order or column-major order. Row major order: In row-major order, we store the el
4 min read
How to declare a Two Dimensional Array of pointers in C?
A Two Dimensional array of pointers is an array that has variables of pointer type. This means that the variables stored in the 2D array are such that each variable points to a particular address of some other element. How to create a 2D array of pointers: A 2D array of pointers can be created following the way shown below. int *arr[5][5]; //creati
3 min read
Changing Array Inside Function in C
When declaring an array the array name is always called the pointer because in an array name the address of the 0th block(base address) is stored. To change an array in the function we have to pass an array in the function. For this, an array_name is passed to the function in the form of an actual argument. So an address of the 0th block pass to th
2 min read
How to Check if Empty Array in C?
Prerequisite: Array in C The array is a type of Data-structure that can store homogeneous data-type elements. The size of the array is fixed. Syntax: int arr[N]; here, the data type of the array is an integer, the name of an array is arr, and the size of the array is N. Example: C/C++ Code // C Program to declare an // array and check it's size #in
3 min read
Why can't a const variable be used to define an Array's initial size in C?
What is an array? An array is a collection of items of the same data type stored at contiguous memory locations. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). The base value is index
3 min read
How to Empty a Char Array in C?
Prerequisite: Char Array in C Char arrays are used for storing multiple character elements in a continuous memory allocated. In a few questions, we have the necessity to empty the char array but You can’t clear a char array by setting the individual members to some value indicating that they don’t contain anything. So, we need some other methods to
4 min read
Dynamic Array in C
Array in C is static in nature, so its size should be known at compile time and we can't change the size of the array after its declaration. Due to this, we may encounter situations where our array doesn't have enough space left for required elements or we allotted more than the required memory leading to memory wastage. To solve this problem, dyna
9 min read
Dynamically Growing Array in C
Prerequisite: Dynamic Memory Allocation in C A Dynamically Growing Array is a type of dynamic array, which can automatically grow in size to store data. The C language only has static arrays whose size should be known at compile time and cannot be changed after declaration. This leads to problems like running out of space or not using the allocated
7 min read
Length of Array in C
The Length of an array in C refers to the number of elements in the array. It must be specified at the time of declaration. It is also known as the size of an array that is used to determine the memory required to store all of its elements. In C Programming language, we don't have any pre-defined function to find the length of the array but we can
4 min read
Maximum Size of an Array in C
Array in C is a collection of elements of the same data type that are stored in contiguous memory locations. The size of an array is determined by the number of elements it can store and there is a limit on this size. The maximum size of an array in C is determined by many factors, including the data type of the array elements, the number of elemen
4 min read
Article Tags :