Flexible Array Member(FAM) is a feature introduced in the C99 standard of the C programming language.
- For the structures in C programming language from C99 standard onwards, we can declare an array without a dimension and whose size is flexible in nature.
- Such an array inside the structure should preferably be declared as the last member of structure and its size is variable(can be changed be at runtime).
- The structure must contain at least one more named member in addition to the flexible array member.
What must be the size of the structure below?
The size of structure is = 4 + 4 + 4 + 0 = 12
In the above code snippet, the size i.e length of array “stud_name” isn’t fixed and is an FAM.
The memory allocation using flexible array members(as per C99 standards) for the above example can be done as:
struct student *s = malloc( sizeof(*s) + sizeof(char [strlen(stud_name)]) );
Note: While using flexible array members in structures some convention regarding actual size of the member is defined.
In the above example the convention is that the member “stud_name” has character size.
For Example, Consider the following structure:
Input : id = 15, name = "Kartik" Output : Student_id : 15 Stud_Name : Kartik Name_Length: 6 Allocated_Struct_size: 18
Memory allocation of above structure:
struct student *s = malloc( sizeof(*s) + sizeof(char [strlen("Kartik")]));
Its structure representation is equal to:
Student_id : 523 Stud_Name : SanjayKanna Name_Length: 11 Allocated_Struct_size: 23 Student_id : 535 Stud_Name : Cherry Name_Length: 6 Allocated_Struct_size: 18 Size of Struct student: 12 Size of Struct pointer: 8
- Adjacent memory locations are used to store structure members in memory.
- In previous standards of the C programming language, we were able to declare a zero size array member in place of a flexible array member. The GCC compiler with C89 standard considers it as zero size array.
This article is contributed by Sanjay Kumar Ulsha from JNTUH College Of Engineering, Hyderabad. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Are array members deeply copied?
- Different ways to Initialize all members of an array to the same value in C
- Initialization of data members
- Static data members in C++
- Can we access private data members of a class without using a member or a friend function?
- Array of Structures vs. Array within a Structure in C/C++
- Difference between Structure and Array in C
- Structure Member Alignment, Padding and Data Packing
- Anonymous Union and Structure in C
- Structure Sorting (By Multiple Rules) in C++
- C | Loops & Control Structure | Question 1
- C | Loops & Control Structure | Question 2
- C | Loops & Control Structure | Question 3
- C | Loops & Control Structure | Question 4
- C | Loops & Control Structure | Question 5
- C | Loops & Control Structure | Question 6
- C | Loops & Control Structure | Question 7
- C | Structure & Union | Question 1
- C | Structure & Union | Question 2
- C | Structure & Union | Question 10
Improved By : Akanksha_Rai