Flexible Array Members in a Structure in C
There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are size flexible.
We need to note that the memory locations, which are usually adjacency in nature, are stored in the due format of the static members. In previous generations of the C programming language, we had the flexibility to declare a zero-sized array which is quite a replacement for a flexible array member. The newer updations have made it possible to consider a variety of sizes zero.
C Language:
Code-1
//This is the C programming language code we have written
//for the concept variable length members in
// structures in GCC to demonstrate using example code as well
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
//the below code depicts that we have created as
//a structure of type student below as a template
struct student
{
int student_id;
int name_length;
//the below code integration to compiler helps to store the variable//variable
//the character array we have used as student_name[]
int struct_size;
//FAM - here we have created a flexible array members like family
//length of the variable length array is should be only
//member of the structure here is
char student_name[];
};
//here we are trying to member initialization and allocation of the //structure
struct student *create_Student(struct student *s,
int id, char a[])
{
//here we are trying to allocate the memory according to the user //provied data
//the array of the characters we are having is
s = malloc( sizeof(*s) + sizeof(char) * strlen(a));
s->student_id = id;
s->name_length = strlen(a);
strcpy(s->student_name, a);
//student name variable assigning according to the size of the variable //we have created
//array[] initialized with variable a with array of characters we are //having it as a copy of the array provided for us
s->struct_size =
(sizeof(*s) + sizeof(char) * strlen(s->student_name));
return s;
}
//from here onwards we are trying to print the details of the students
void print_Student(struct student *s)
{
printf("Student_id : %d\n"
"Stud_Name : %s\n"
"Name_Length: %d\n"
"Allocated_Struct_size: %d\n\n",
s->student_id, s->student_name, s->name_length,
s->struct_size);
//structure size which is in bytes is the allocated from here onwards
}
//the essential driver code starts from here
int main()
{
struct student *s1 = create_Student(s1, 923, "ram charan teja");
struct student *s2 = create_Student(s2, 635, "pawan kalyan sanjay");
print_Student(s1);
print_Student(s2);
//structure size of the student will be clalculated here
printf("structure student size we are having as : %lu\n",
sizeof(struct student));
//size of the pointer which is the structure pointer here is
printf("structure pointer size we are having it as : %lu",
sizeof(s1));
//c programming language code ends from here
return 0;
}
Output:
Student_id : 923
Stud_Name : ram charan teja
Name_Length: 15
Allocated_Struct_size: 27
Student_id : 635
Stud_Name : pawan kalyan sanjay
Name_Length: 19
Allocated_Struct_size: 31
Size of Struct student: 12
Size of Struct pointer: 8
C Language:
Code-2
//This is the C programming language code we have written
//for the concept variable length members in
// structures in GCC to demeonstrate using example code as well
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
//the below code depicts that we have created as
//a structure of type student below as a template
struct employee
{
int employee_id;
int name_length;
//the below code integration to compiler helps to store the variable//variable
//the character array we have used as student_name[]
int struct_size;
//FAM - here we have created a flexible array member like family
//length of the variable length array is should be only
//member of the structure here is
char employee_name[];
};
//here we are trying to member initialization and allocation of the //structure
struct employee *create_employee(struct employee *s,
int id, char a[])
{
//here we are trying to allocate the memory according to the user //provied data
//the array of the characters we are having is
s = malloc( sizeof(*s) + sizeof(char) * strlen(a));
s->employee_id = id;
s->name_length = strlen(a);
strcpy(s->employee_name, a);
//employee name variable assigning according to the size of the variable //we have created
//array[] initialized with variable a with array of characters we are //having it as a copy of the array provided for us
s->struct_size =
(sizeof(*s) + sizeof(char) * strlen(s->employee_name));
return s;
}
//from here onwards we are trying to print the details of the employee
void print_employee(struct employee *s)
{
printf("employee_id : %d\n"
"employee_Name : %s\n"
"Name_Length: %d\n"
"Allocated_Struct_size: %d\n\n",
s->employee_id, s->employee_name, s->name_length,
s->struct_size);
//structure size which is in bytes is the allocated from here onwards
}
//the essential driver code starts from here
int main()
{
struct employee*s1 = create_employee(s1, 23, "teja allapati");
struct employee*s2 = create_employee(s2, 35, "nitin chowdary");
print_employee(s1);
print_employee(s2);
//structure size of the employee will be clalculated here
printf("structure employee size we are having as : %lu\n",
sizeof(struct employee));
//size of the pointer which is structure pointer here is
printf("structure pointer size we are having it as : %lu",
sizeof(s1));
//c programming language code ends from here
return 0;
}
Output:
employee_id : 23
employee_Name : teja allapati
Name_Length: 13
Allocated_Struct_size: 25
employee_id : 35
employee_Name : nitin chowdary
Name_Length: 14
Allocated_Struct_size: 26
structure employee size we are having as : 12
structure pointer size we are having it as : 8