×

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

Related Topics

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

4 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

User Defined Functions in C

C Functions: A function is defined as a block of code that is used to perform some specific task. In functions, we use flower brackets ({}) which contain the set of programming...

5 minutes read.

If else Programs in C Programming

If else Programs in C programming The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in...

4 minutes read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

Different ways to Declare the Variable as Constant in C

There are a wide range of ways of making the variable as steady Using const keyword: The const catchphrase permits a software engineer to inform the compiler that a specific variable should...

5 minutes read.

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

3 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.

Git Hooks

Git Hooks Overview Just like other version control systems, Git has its way to deliver customized scripts whenever some important activity occurs. The hooks act just like the triggers or catalysts...

4 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Dangling pointers in C

Dangling pointers in C: A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are...

4 minutes read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.