赞
踩
结构体的使用:
#include <stdio.h> struct Point{ int x; int y; int z; }; void PrintPoint(struct Point m){ printf("(%d,%d,%d)\n",m.x,m.y,m.z); } void PrintPointPtr(struct Point* p){ printf("(%d,%d,%d)\n",p->x,p->y,p->z); } int main(){ struct Point a = { 1,2,3}; //结构体打印方法 printf("(%d,%d,%d)\n",a.x,a.y,a.z); //结构体传参 PrintPoint(a); //结构体输入 scanf("%d%d%d",&a.x,&a.y,&a.z); PrintPoint(a); //结构体指针 printf("&a = %p\n",&a); struct Point* p = &a; printf("p = %p\n",p); //结构体指针解引用 printf("x = %d\n",(*p).x); printf("y = %d\n",(*p).y); printf("z = %d\n",(*p).z); //结构体指针解引用简写 printf("x = %d\n",p->x); printf("y = %d\n",p->y); printf("z = %d\n",p->z); //结构体指针传参 PrintPointPtr(p); }
结果为:
(1,2,3)
(1,2,3)
4 5 6
(4,5,6)
&a = 0x7ffd84170d6c
p = 0x7ffd84170d6c
x = 4
y = 5
z = 6
x = 4
y = 5
z = 6
(4,5,6)
结构体可以直接赋值操作
对结构体数组输出,qsort 排序结构体数组排序:
#include <stdio.h> #include <stdlib.h> struct Point{ int x; int y; int z; }; void PrintPoint(struct Point p){ printf("(%d,%d,%d)\n",p.x,p.y,p.z); } void PrintPointPtr(struct Point* p){ printf("(%d,%d,%d)\n",p->x,p->y,p->z); } int cmp(const void* a,const void* b){ struct Point* p = (struct Point*)a; struct Point* q = (struct Point*)b; return p->y <
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。