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
Declaring a structure with no members in C language
In C programming, it is possible to declare a structure with no members. This creates what is known as an empty structure or zero-size structure. While not commonly used in practice, understanding this concept helps in grasping how C handles structure memory allocation.
Syntax
struct structure_name {
// No members declared
};
Size of Empty Structure
When a structure is declared with no members, its size is implementation-defined. In most C compilers, the size of an empty structure is 0 bytes, but some compilers may assign a minimal size (like 1 byte) to ensure each structure instance has a unique address in memory.
Example
Here's a complete program demonstrating an empty structure −
#include <stdio.h>
// Structure with no members
struct empty_struct {
};
int main() {
// Declaring structure variable
struct empty_struct T;
printf("Size of empty structure: %zu bytes
", sizeof(T));
printf("Size using struct name: %zu bytes
", sizeof(struct empty_struct));
return 0;
}
Size of empty structure: 0 bytes Size using struct name: 0 bytes
Practical Considerations
- Memory allocation: Empty structures consume no memory but still have valid addresses
- Compiler behavior: Different compilers may handle empty structures differently
- Usage: Rarely used in practice, mainly for placeholder purposes or advanced template programming
Key Points
- Empty structures are legal in C but have limited practical use
- The
sizeof()operator returns 0 for most implementations - Each instance of an empty structure still has a unique memory address
Conclusion
While C allows declaring structures with no members, resulting in zero-size structures, this feature is rarely used in practical programming. Understanding this concept helps in comprehending C's flexible structure definition capabilities.
