Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Data Types we cannot use to create array in C
In C programming, arrays can be created using most data types like int, char, float, double, etc. However, there are certain data types that cannot be used to create arrays. The most notable restriction is with the void data type.
Syntax
datatype array_name[size]; // Valid for most data types void array_name[size]; // Invalid - compilation error
Data Types That Cannot Be Used for Arrays
1. Void Data Type
The void data type represents "no type" and has no size. Since arrays need to know the size of each element to allocate memory, void arrays are not permitted −
#include <stdio.h>
int main() {
/* This will cause a compilation error */
void arr1[4];
printf("This will not compile<br>");
return 0;
}
error: declaration of 'arr1' as array of voids void arr1[4];
2. Incomplete Types
Arrays cannot be created with incomplete types (types whose size is not known at compile time) −
#include <stdio.h>
int main() {
/* Valid array declarations */
int numbers[5];
char letters[10];
float values[3];
printf("Valid arrays created successfully<br>");
printf("Size of int array: %zu bytes<br>", sizeof(numbers));
printf("Size of char array: %zu bytes<br>", sizeof(letters));
printf("Size of float array: %zu bytes<br>", sizeof(values));
return 0;
}
Valid arrays created successfully Size of int array: 20 bytes Size of char array: 10 bytes Size of float array: 12 bytes
Why Void Arrays Are Not Allowed
-
No defined size:
voidhas no specific size, making memory allocation impossible - Type safety: Arrays need a specific type for element access and pointer arithmetic
- Memory layout: The compiler cannot determine how much memory each element requires
Conclusion
While C allows arrays of most data types, void and incomplete types cannot be used for array creation due to their undefined size. Always use complete data types with known sizes when declaring arrays.
Advertisements
