c:struct之匿名struct
一、代码:
/*
* gcc11.1(c17)
*
*/
#include <stddef.h>
#include <stdio.h>
// 定义常规struct和指针struct
typedef struct
{
// 匿名struct的定义
struct{ char *name; char *id; int score; };
char *date;
}info, *ptinfo;
// 匿名struct初始化
info lidawei={.name="laohu", .id="13572468", .score=145, .date="2022-07-04"};
ptinfo pf=&lidawei;
void msg(ptinfo in)
{
printf("id=%s,\tname=%s, \tscore=%d, \tdate=%s\t",
in->id, in->name, in->score, in->date);
}
int main(int argc, char *argv[])
{
int max = argc;
printf("argc=%d\n", argc);
for(int i=0; i<max; i++)
{
printf("argv[%d]=%s\n", i, argv[i]);
}
msg(pf);
return 0;
}
/*
* 运行结果:
*
* argc=1
* argv[0]=./a.out
* id=13572468, name=laohu, score=145, date=2022-07-04
*/