解释 C 编程中结构内数组的概念
C 编程中结构的数组是不同数据类型变量的集合,集合在单一的名称下。
结构声明的一般形式
结构声明如下 −
struct tagname{
datatype member1;
datatype member2;
datatype member n;
};此处,struct 是关键字。
tagname 指定结构的名称。
member1, member2 指定组成结构的数据项。
示例
以下示例展示了 C 编程中,结构中的数组的使用 −
struct book{
int pages;
char author [30];
float price;
};示例
以下是用来演示结构中数组使用的 C 程序 −
#include <stdio.h>
// Declaration of the structure candidate
struct candidate {
int roll_no;
char grade;
// Array within the structure
float marks[4];
};
// Function to displays the content of
// the structure variables
void display(struct candidate a1){
printf("Roll number : %d
", a1.roll_no);
printf("Grade : %c
", a1.grade);
printf("Marks secured:
");
int i;
int len = sizeof(a1.marks) / sizeof(float);
// Accessing the contents of the
// array within the structure
for (i = 0; i < len; i++) {
printf("Subject %d : %.2f
",
i + 1, a1.marks[i]);
}
}
// Driver Code
int main(){
// Initialize a structure
struct candidate A= { 1, 'A', { 98.5, 77, 89, 78.5 } };
// Function to display structure
display(A);
return 0;
}输出
当执行以上程序时,产生以下结果 −
Roll number : 1 Grade : A Marks secured: Subject 1 : 98.50 Subject 2 : 77.00 Subject 3 : 89.00 Subject 4 : 78.50
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP