1.创建声明结构体
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
int main()
{
struct student record = {0}; //Initializing to null
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
return 0;
}
输出
Id is: 1
Name is: Raju
Percentage is: 86.500000
2.声明结构体变量2
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
} record;
int main()
{
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
return 0;
}
3.单独定义
structure.h
struct student
{
int id;
char name[20];
float percentage;
} record;
structure.c
// File name - structure.c
#include <stdio.h>
#include <string.h>
#include "structure.h" /* header file where C structure is
declared */
int main()
{
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
return 0;
}
4.指针定义
struct student rep = {100, “Mani”, 99.5};
report = &rep;
report -> mark;
report -> name;
report -> average;