@yanglt7
2018-08-06T00:09:57.000000Z
字数 4616
阅读 797
C
10.1.1 枚举:枚举
10.2.1 结构:结构类型
#include <stdio.h>
int main(int argc, char const *argv[])
{
struct date{
int month;
int day;
int year;
};
struct date today;
today.month = 04;
today.day = 16;
today.year = 2018;
printf("Today's date is %i-%i-%i.\n",
today.year,today.month,today.day);
return 0;
}
Today's date is 2018-4-16.
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的值
#include <stdio.h>
int main(int argc, char const *argv[])
{
struct date{
int month;
int day;
int year;
};
struct date today;
today=(struct date){04,16,2018};
struct date *pDate = &today;
printf("Today's date is %i-%i-%i.\n",
today.year,today.month,today.day);
printf("address of today is %p\n", pDate);
return 0;
}
10.2.2 结构:结构与函数
#include <stdio.h>
struct point {
int x;
int y;
};
void getStruct(struct point);
void output(struct point);
int main(int argc, char const *argv[])
{
struct point y = {0,0};
getStruct(y);
output(y);
}
void getStruct(struct point p)
{
scanf("%d", &p.x);
scanf("%d", &p.y);
printf("%d, %d\n", p.x, p.y);
}
void output(struct point p)
{
printf("%d, %d", p.x, p.y);
}
12 23
12,23
0,0
#include <stdio.h>
struct point {
int x;
int y;
};
struct point getStruct(void);
void output(struct point);
int main(int argc, char const *argv[])
{
struct point y = {0,0};
y = getStruct();
output(y);
}
struct point getStruct(void)
{
struct point p;
scanf("%d", &p.x);
scanf("%d", &p.y);
printf("%d, %d\n", p.x, p.y);
return p;
}
void output(struct point p)
{
printf("%d, %d", p.x, p.y);
}
12 23
12, 23
12, 23
struct date {
int month;
int day;
int year;
} myday;
struct date *p = &myday;
(*p).month = 12;
p -> month = 12;
10.2.3 结构:结构中的结构
struct dateAndTime {
struct date sdate;
struct time stime;
};
struct point {
int x;
int y;
};
struct rectangle {
struct point pt1;
struct point pt2;
};
如果有变量
struct rectangle r;
就可以有:
r.pt1.x、 r.pt1.y,
r.pt2.x 和 r.pt2.y
如果有变量定义:
struct rectangle r, *rp;
rp = &r;
那么下面的四种形式是等价的:
r.pt1.x
rp->pt1.x
(r.pt1).x
(rp->pt1).x
但是没有rp->pt1->x (因为pt1不是指针)
#include <stdio.h>
struct point{
int x;
int y;
};
struct rectangle {
struct point p1;
struct point p2;
};
void printRect(struct rectangle r)
{
printf("<%d, %d> to <%d, %d>\n", r.p1.x, r.p1.y, r.p2.x, r.p2.y);
}
int main(int argc, char const *argv[])
{
int i;
struct rectangle rects[] = {
{{1,2}, {3,4}},
{{5,6}, {7,8}}
};// 2 rectangles
for(i=0;i<2;i++) {
printRect(rects[i]);
}
}
<1, 2> to <3, 4>
<5, 6> to <7, 8>
10.3.1 联合:类型定义
typedef long int64_t; //重载已有的类型名字,新名字的含义更清晰,具有可移植性
typedef struct ADate {
int month;
int day;
int year;
} Date; //简化了复杂的数字
int 64_t i = 1000000000;
Date d = {4,17,2018};
typedef int Length;//Length 就等价于int类型
typedef *char[10] Strings;//Strings 是10个字符串的数组的类型
typedef struct node {
int data;
struct node *next;
} aNode;
或
typedef struct node aNode;//这样用aNode就可以代替struct node
10.3.2 联合:联合
union AnElt {
int i;
char c;
} elt1,elt2;
elt1.i = 4;
elt2.c = 'a';
elt2.i = 0xDEADBEEF;
#include <stdio.h>
typedef union {
int i;
char ch[sizeof(int)];
} CHI;
int main(int argc, char const *argv[])
{
CHI chi;
int i;
chi.i = 1234;
for (i=0; i<sizeof(int); i++) {
printf("%02hhX", chi.ch[i]);
}
printf("\n");
return 0;
}
FFD2040000