C语言中结构中的灵活数组成员

灵活数组成员(FAM)是C编程语言的C99标准中引入的一项功能。

null
  • 对于 结构 在C99标准之后的C编程语言中,我们可以声明一个数组 没有维度 而且它的大小在本质上是灵活的。
  • 最好将结构中的这样一个数组声明为 最后一名成员 结构及其大小是可变的(可以在运行时更改)。
  • 除了灵活的数组成员外,该结构还必须至少包含一个命名成员。

下面的结构必须有多大?

struct student
{
int stud_id;
int name_len;
int struct_size;
char stud_name[];
};


The size of structure is = 4 + 4 + 4 + 0 = 12

在上面的代码片段中,数组“stud_name”的大小即长度不是固定的,而是一个FAM。

对于上述示例,使用灵活阵列成员(根据C99标准)的内存分配可以按如下方式进行:

 struct student *s = malloc( sizeof(*s) + sizeof(char [strlen(stud_name)])  );

注: 在结构中使用灵活的数组成员时,定义了有关成员实际大小的一些约定。 在上面的示例中,约定是成员“stud_name”具有字符大小。

例如,考虑以下结构:

Input : id = 15, name = "Kartik" 
Output : Student_id : 15
         Stud_Name  : Kartik
         Name_Length: 6
         Allocated_Struct_size: 18

上述结构的内存分配:

struct student *s = 
        malloc( sizeof(*s) + sizeof(char [strlen("Kartik")]));

其结构表示等于:

struct student
{
int stud_id;
int name_len;
int struct_size;
char stud_name[6]; //character array of length 6
};


实施

// C program for variable length members in
// structures in GCC
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
// A structure of type student
struct student
{
int stud_id;
int name_len;
// This is used to store size of flexible
// character array stud_name[]
int struct_size;
// Flexible Array Member(FAM)
// variable length array must be last
// member of structure
char stud_name[];
};
// Memory allocation and initialisation of structure
struct student *createStudent( struct student *s,
int id, char a[])
{
// Allocating memory according to user provided
// array of characters
s =
malloc ( sizeof (*s) + sizeof ( char ) * strlen (a));
s->stud_id = id;
s->name_len = strlen (a);
strcpy (s->stud_name, a);
// Assigning size according to size of stud_name
// which is a copy of user provided array a[].
s->struct_size =
( sizeof (*s) + sizeof ( char ) * strlen (s->stud_name));
return s;
}
// Print student details
void printStudent( struct student *s)
{
printf ( "Student_id : %d"
"Stud_Name : %s"
"Name_Length: %d"
"Allocated_Struct_size: %d" ,
s->stud_id, s->stud_name, s->name_len,
s->struct_size);
// Value of Allocated_Struct_size is in bytes here
}
// Driver Code
int main()
{
struct student *s1 = createStudent(s1, 523, "Cherry" );
struct student *s2 = createStudent(s2, 535, "Sanjayulsha" );
printStudent(s1);
printStudent(s2);
// Size in struct student
printf ( "Size of Struct student: %lu" ,
sizeof ( struct student));
// Size in struct pointer
printf ( "Size of Struct pointer: %lu" ,
sizeof (s1));
return 0;
}


输出:

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

要点:

  1. 相邻的内存位置用于在内存中存储结构成员。
  2. 在以前的C编程语言标准中,我们可以声明一个零大小的数组成员来代替灵活的数组成员。C89标准的GCC编译器将其视为零大小数组。

本文由 桑杰·库马尔·乌尔沙 从…起 海得拉巴JNTUH工程学院 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享