c语言入门教程 第12讲 结构体

结构体

可以将多种数据类型组合起来的结构

声明方式

struct 结构体名称{
成员1的类型  成员1的名称;
成员2的类型 成员2的名称;
……
成员3的类型  成员3的名称;
} ;

举例:

struct time{
	int hour;
	int minute;
	int second;
} ;

注意不要漏掉了最后的分号

结构体的定义

1)常规定义

struct time{
	int hour;
	int minute;
	int second;
} ;

struct time t;

如何定义一个int类型的变量?

	int number;

实际上来说,结构体变量的定义是跟普通数据类型类似的。
如,以上两个变量的声明区别,仅在于,一个的类型是struct time 另一个的类型是int

2)声明的同时定义

struct student{
	char name[256];
	char sex[2];
	int age;
	int grade;
} Alan, Tom ;

3)使用结构体作为成员

struct DATE{
	int year;
	int month;
	int day;
} ;

struct person{
	char name[256];
	struct DATE birthday;
} ;

struct time t;

4)匿名结构体

struct {
	int number;
	char name[256];
	char sex[2];
	int age;
	int grade;
} Alan, Tom ;

结构体的引用与初始化

使用“.”成员运算符来获取结构体中的成员

#include<stdio.h>#include<string.h>

struct student{
	int number;
	char name[256];
	char sex[2];
	int age;
	int grade;
} ;

main()
{
	struct student alan;
	alan.number = 001;
	strcpy(alan.name, "Allan");
	strcpy(alan.sex, "男");
	alan.age = 18;
	alan.grade = 3;

	printf("学号:%d n姓名: %s n性别:%s n年龄:%dn年级:%dn", 
		   alan.number, alan.name, alan.sex, alan.age, alan.grade);
}

结构体数组

结构体数组的使用:

#include<stdio.h>
#include<string.h>

struct student{
	int number;
	char name[256];
	int age;
} ;

main()
{
	struct student class_02[5];
	int k;

	for(k=0; k < 5; k++)
	{
		printf("Please input number:n");
		scanf("%d", &class_02[k] .number);
		printf("Please input name:n");
		scanf("%s", class_02[k] .name);

		printf("Please input age:n");
		scanf("%d", &class_02[k] .age);

		printf("n");
	}

	printf("NumtNametagen");

	for(k=0; k < 5; k++)
	{
		printf("%dt", class_02[k] .number);
		printf("%st", class_02[k] .name);
		printf("%dn", class_02[k] .age);
	}
}
#include<stdio.h>

struct student{
	int number;
	char name[256];
	char sex[6];
	int age;
	int grade;
} ;

main()
{
	struct student class1[5] = {
		{ 01, "Alan", "man", 16, 12 },
		{ 02, "Bob", "man", 18, 12 },
		{ 03, "Cici", "woman", 16, 12 },
		{ 04, "David", "man", 11, 12 },
		{ 05, "Elis", "woman", 16, 12 },
	};
	for(k = 0; k < 5; k++)
	{
		printf("nstudy number: %d nname: %s nsex: %s nage: %dn grade: %dn" ,class1[k].number,
		class1[k].name,
		class1[k].sex,
		class1[k].age,
		class1[k].grade);
	}
}

结构体指针

使用分量运算符“->”来获取成员

#include<stdio.h>

struct DATE{
	int year;
	int month;
	int day;
} *date ;

main()
{
	date->year = 2012;
	printf("%d", date->year);
}

#include<stdio.h>

struct DATE{
	int year;
	int month;
	int day;
} date = { 2012, 11, 27 }, *d ;

main()
{
	d = &date;
	printf("%d", d->year);
}

用于函数的参数以及返回值:

#include<stdio.h>

struct time add(struct time now, struct time pass);

struct time{
	int hour;
	int min;
} ;

main()
{
	struct time now = { 3, 55 }, pass = { 1, 33 }, result;
	result = add(now, pass);
	printf("%d:%dn", result.hour, result.min);
}

struct time add(struct time now, struct time pass)
{
	struct time rel;
	rel.hour = now.hour + pass.hour + (now.min + pass.min)/60 ;
	rel.min = (now.min + pass.min)%60;
	return rel;
}
Advertisements

1 thought on “c语言入门教程 第12讲 结构体

  1. 结构体指针那里有点错误。
    #include

    struct DATE{
    int year;
    int month;
    int day;
    } *date ;

    main()
    {
    date->year = 2012;
    printf(“%d”, date->year);
    }
    试了半天,都不行,要改成
    #include

    struct DATE{
    int year;
    int month;
    int day;
    } *date ;

    main()
    {
    date=malloc(sizeof(struct DATE));
    date->year = 2012;

    printf(“%d”, date->year);
    }

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

You are commenting using your WordPress.com account. Log Out /  更改 )

Google+ photo

You are commenting using your Google+ account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

Connecting to %s