这个案例文件包含两个源文件和一个头文件
头文件Strct.h中包含了结构体的定义和函数申明:
#include <stdio.h>
struct ArrStr
{
int a[3];
char b[6];
};
struct ArrStr StrFnctn();
源文件Main.c中包含数组结构体的定义、赋值及函数的调用:
#include <stdio.h>
#include <string.h>
#include "Strct.h"
int main()
{
struct ArrStr MyStrct;
MyStrct.a[0] = 1;
MyStrct.a[1] = 5;
MyStrct.a[2] = 8;
strcpy(MyStrct.b, "Hello");
printf("%d, %d, %d\n", MyStrct.a[0], MyStrct.a[1], MyStrct.a[2]);
printf("%s\n", MyStrct.b);
struct ArrStr MyStrct1;
MyStrct1 = StrFnctn();
printf("%d, %d, %d\n", MyStrct1.a[0], MyStrct1.a[1], MyStrct1.a[2]);
printf("%s", MyStrct1.b);
}
源文件StrFnctn.c中包含了一个返回结构体的自定义函数:
#include <stdio.h>
#include "string.h"
#include "Strct.h"
struct ArrStr StrFnctn()
{
struct ArrStr Arr1;
Arr1.a[0] = 55;
Arr1.a[1] = 36;
Arr1.a[2] = 89;
strcpy(Arr1.b, "Name");
return Arr1;
}
运行结果:
源代码和项目文件在这里
StrctArryRtrn.rar