结构类型:
#include<stdio.h>
int main(int argc,char const *argv[])
{
struct date{
int month;
int day;
int year;
};
struct date today;
today.month = 07;
today.day = 31;
today.year = 2014;
printf("Today's date is %i-%i-%i.\n",
today.year,today.month,today.day);
return 0 ;
}
和本地变量一样,在函数内部声明的结构类型只能在函数内部使用,所以通常在函数外部声明结构类型,这样就可以被多个函数所使用了
声明结构的形式:
struct point {
int x;
int y ;
};
struct point p1,p2; //p1和p2都是point里面有X,Y的值
struct {
int x;
int y ;
}p1,p2;//p1和p2都是一种无名结构,里面有x和y
struct point {
int x;
int y ;
}p1,p2;//p1和p2都是point里面有X和Y的值T
对于第一和第三种形式,都声明了结构point。但是第二种形式没有声明point,只是定义了两个变量。
结构类型和结构变量是两件事情
#include<stdio.h>
struct date{
int month;
int day;
int year;
};//声明 一种新的类型
int main(int argc,char const *argv[])
{
struct date today;//定义 这种结构类型的一个变量;
today.month = 07;
today.day = 31;
today.year = 2014;
printf("Today's date is %i-%i-%i.\n",
today.year,today.month,today.day);
return 0 ;
}
结构变量的初始化:
#include<stdio.h>
struct date{
int month;
int day;
int year;
};
int main(int argc,char const *argv[])
{
struct date today= {
07,31,2014
}; //第一种初始方式,依次被赋予
struct date thismonth = {
.month=7, .year=2014
};//指定被赋予值
printf("Today's date is %i-%i-%i.\n",
today.year,today.month,today.day