易百教程

C语言中结构是什么?

C语言中结构是:

  • 结构是一种用户定义的数据类型,允许在一个单元中存储多种类型的数据。它占据了所有成员的内存之和。
  • 结构成员只能通过结构变量来访问。
  • 结构变量访问同一结构,但为每个变量分配的内存将是不同的。

语法:

struct structure_name  
{  
  Member_variable1;  
 Member_variable2  
.  
.  
}[structure variables];

示例代码:

#include <stdio.h>  
struct student  
{  
    char name[10];       // structure members declaration.  
    int age;  
}s1;      //structure variable  
int main()  
{  
    printf("Enter the name");  
    scanf("%s",s1.name);  
    printf("\n");  
    printf("Enter the age");  
    scanf("%d",&s1.age);  
    printf("\n");  
    printf("Name and age of a student: %s,%d",s1.name,s1.age);  
    return 0;  
}

运行上面示例代码,得到以下结果 -

Enter the name yiibai
Enter the age 23
Name and age of a student: yiibai,23