当前位置:网站首页>Anonymous structure in C language

Anonymous structure in C language

2022-07-05 08:05:00 Car chezi

stay C Of C11 In the standard , Added anonymous structure and anonymous union .
Anonymous Union / Structures are also known as unnamed unions / structure , Because there is no name , Therefore, their direct objects will not be created ( Or variable ), Usually we use them in nested structures or unions .

An anonymous struct declaration is a declaration that declares neither a tag for the struct, nor an object or typedef name. Anonymous structs are not allowed in C++.

give an example :

// C Program to demonstrate working of anonymous struct
#include <stdio.h> 
struct scope
{
    
	// Anonymous structure
	struct 
	{
    
		char alpha;
		int num;
	};
};

int main(void)
{
    
	struct scope x;
	x.num = 65;
	x.alpha = 'B';

	// Note that members of structure are accessed directly
	printf("x.alpha = %c, x.num = %d\n", x.alpha, x.num);

	return 0;
}

Reference material
【1】https://www.geeksforgeeks.org/g-fact-38-anonymous-union-and-structure/
【2】https://docs.oracle.com/cd/E19205-01/819-5267/bkael/index.html

原网站

版权声明
本文为[Car chezi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140546257798.html