×

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

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given...

4 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

C Program for Extended Euclidean algorithms

The Euclidean method simply computes the GCD (greatest common divisor) of two numbers, p and q. (Let's assume) The GCD of two integers is the greatest number that divides them both....

3 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

Built-in functions in C

The function is a set of instructions and statements enclosed in the "{}" delimiter. In c, there are two types of functions. Pre-define functions/ Built-in functionsUser define function. Built-in functions in C:- These...

8 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.