What are the data types for which it is not possible to create an array?
In C, an array is a collection of variables of the same data type, stored in contiguous memory locations. Arrays can store data of primitive types like integers, characters, and floats, as well as user-defined types like structures.
However, there are certain data types for which arrays cannot be directly created due to the nature of those types. Data types for which arrays cannot be created are:
Table of Content
Function Types
In C, we cannot create an array of functions, as the function type is not a variable type. Functions are used to define behaviour, not to hold data. Doing so will throw an error as shown in the given example:
#include <stdio.h>
void func() {
// Some code
}
int main() {
// Create an array
void (*arr)[1]() = {func};
}
Output
solution.c: In function ‘main’:
solution.c:10:12: error: declaration of ‘arr’ as array of functions
10 | void (*arr)[1]() = {func};
| ^~~
Explanation: The compiler cannot treat a function as a data object that can be stored in memory like integers or floats. Instead, functions are referenced by their addresses when needed, but they cannot be stored as array elements.
Void Type
The void data type represents the absence of a value, and it cannot hold any data. Arrays require a specific type of data to store, and void does not hold any value to be stored in an array.
Let's take a look at an example:
#include <stdio.h>
void func() {
// Some code
}
int main() {
// Create an array
void arr[10];
return 0;
}
Output
solution.c: In function ‘main’:
solution.c:10:10: error: declaration of ‘arr’ as array of voids
10 | void arr[10];
| ^~~
Explanation: A void type is used for functions that do not return a value or for pointers, but it cannot be used to store a sequence of elements, as it lacks a data representation.