C语言结构体2-结构体的嵌套

结构体 2

#include "stdio.h"
#include "assert.h"
#include "string.h"
#define NAME_LEN 20 

//结构体的嵌套结构 

struct person_name{
    char first[NAME_LEN+1];//
    char last[NAME_LEN+1];      
};


struct student{
    struct person_name name;
    int id,age;
    char sex;
} student1,student2;
//访问student1需要两次运用运算符
//strcpy(student1.name.first,"firdayy"); 
/*
struct person_name new_name;
---
student1.name = new_name; //直接赋值 

*/ 

struct student WritDate( const char*name, int id, int age); 
int main()
{
    struct student stu;
    stu = WritDate("guy",56,99);
    printf("%s\n",stu.name.first);
    printf("%d\n",stu.id);




    printf("\nHello world !\n");
    return 0;
} 

struct student WritDate(const char*name, int id, int age)
{
    struct student stu;
     stu.age=age;
     stu.id=id;

     strcpy(stu.name.first, name);
     strcpy(stu.name.last, name);
     return stu;    
} 

猜你喜欢

转载自blog.csdn.net/qq_32460819/article/details/81263801