对结构体数组的初步学习

开始

[作者:技术者高健@博客园  mail: luckyjackgao@gmail.com ]

[root@localhost test]# cat teststr.c
#include<stdio.h>
#include<stdlib.h>

int main()
{

    struct person
    {
       char name[8];
       int age;
       char sex[4];
       char depart[20];
    };

    struct person student;

    struct person class[]=
    {
       {
          "Tom",
          23,
          "man",
          "product"
       },

       {
          "Jack",
          25,
          "wom",
          "R&D" 
       }
    };

    fprintf(stderr,"first is: %s\n", class[0].name);
    return 0;
}
[root@localhost test]# 

运行结果:

[root@localhost test]# gcc -o teststr teststr.c
[root@localhost test]# ./teststr
first is: Tom

[作者:技术者高健@博客园  mail: luckyjackgao@gmail.com ]

并且,我们可以发现,不完全匹配也是可以的:

[root@localhost test]# cat teststr.c
#include<stdio.h>
#include<stdlib.h>

int main()
{

    struct person
    {
       char name[8];
       int age;
       char sex[4];
       char depart[20];
    };

    struct person student;

    struct person class[]=
    {
       {
          "Tom",
          23,
          "man"
       },

       {
          "Jack",
          25,
          "wom"
       }
    };

    fprintf(stderr,"first is: %s\n", class[0].name);
    return 0;
}
[root@localhost test]# gcc -o teststr teststr.c
[root@localhost test]# ./teststr
first is: Tom
[root@localhost test]# 

结束

原文地址:https://www.cnblogs.com/gaojian/p/2750628.html