标签:
一、结构体指针
1)结构体指针 指针指向结构体的存储单元 先对结构体的变量取地址
2)p指向结构体的起始地址 就是首个成员变量的地址
typedef struct teacher{ char name[20]; int age; }Teacher; int main(int argc, const char * argv[]) { Teacher t1 = {"laoluo",20}; Teacher *p = &t1; printf("p = %p,name = %p,age = %p\n",p,t1.name,&t1.age); return 0; }
3) 通过结构体指针访问成员变量
(*P).name (*p):p指向结构体的存储单元
typedef struct teacher{ char name[20]; int age; }Teacher; int main(int argc, const char * argv[]) { Teacher t2 = {"hahazheng",23}; Teacher *p1 = &t2; strcpy((*p1).name, "memeda"); (*p1).age = 24; printf("%s %d\n",(*p1).name,(*p1).age); return 0; }
4) 通过箭头访问 p -> name;
typedef struct teacher{ char name[20]; int age; }Teacher; int main(int argc, const char * argv[]) { Teacher t3 = {"heheda",22}; Teacher *p1 = &t3; p1 -> age = 20; printf("%s %d\n",p1->name,p1->age); return 0; }
5) 求坐标两点之间的距离
typedef struct cpoint{ float x; float y; }CPoint; float TwoPoint(CPoint *ps,CPoint *pd){ float dx = ps->x - pd->x; float dy = ps->y - pd->y; float dz = sqrtf(dx*dx + dy*dy); return dz; } int main(int argc, const char * argv[]) { CPoint m ={0.0,4.0}; CPoint n = {3.0,0.0}; CPoint *ps = &m; CPoint *pd = &n; printf("两点之间的距离为:%f",TwoPoint(ps, pd)); return 0; }
6)字符串操作
typedef struct student{ int number; char name[20]; char gender; float score; }Student; int main(int argc, const char * argv[]) { Student stu = {1,"lan ou",‘m‘,95.6}; Student *pl = &stu ; int length = (int)strlen(pl->name); for (int i =0; i < length ; i++) { if (i == 0 && (*(pl->name) >=‘a‘ && *(pl->name)<=‘z‘)){ //pl->name拿到name字符串 因为自己字符串本身是有地址的 [Student *pl = &stu][拿到首地址] 所以(*(pl->name))就是拿到字符串的首地址(首字母) *(pl->name) = *(pl->name) - 32; } if (*((pl->name)+i) == ‘ ‘) { *((pl->name)+i) =‘_‘; } } printf("%s\n",pl->name); return 0; }
二、结构体数组
typedef struct student{ char name[20]; int age; char gender; float score; }Student; int main(int argc, const char * argv[]) { Student stu3[3] = {{"liubei",100,‘m‘,23},{"guanyu",17,‘m‘,90},{"zhangfei",18,‘m‘,78}}; Student *ss = stu3; printf("%s\n",stu3[0].name); printf("%s\n",ss[0].name); printf("%s\n",ss->name); printf("%f\n",(*(ss+1)).score); printf("%d\n",(*(ss+2)).age); printf("%f\n",(ss+2)->score); printf("%d\n",(int)sizeof(Student)); for (int i =0; i<3; i++) { printf("%s %d %c %.2f\n",(ss+i)->name,(ss+i)->age,(ss+i)->gender,(ss+i)->score); } return 0; }
三、结构件指针作为函数参数
typedef struct person{ char name[20]; int age; char gender; float weight; }Person; typedef struct student{ char name[20]; int age; char gender; float score; }Student; void maxWeight(Person *p3,Person *p4){ Person *max = p3->weight > p4->weight ? p3:p4;//p3 p4指向结构体的起始地址 就是首个成员变量的地址 所以再将它赋值给新的指针变量时 并不需要重新获得地址 printf("%s %d %c %.2f\n",max->name,max->age,max->gender,max->weight); } int main(int argc, const char * argv[]) { Person p1 = {"gailun",18,‘m‘,110}; Person p2 = {"liqing",30,‘m‘,125}; Person *p3 = &p1; Person *p4 = &p2; maxWeight(p3, p4); Student stu1 = {"chengmanxiang",27,‘f‘,99}; Student *s = &stu1; (*s).age = 18; s->gender = ‘m‘; printf("%c\n",(*s).gender); printf("%s\n",s->name); return 0; }
四、结构体数组作为函数参数
typedef struct student{ char name[20]; int age; char gender; float score; }Student; void printStudent(Student *stu,int n){ for (int i =0; i<n; i++) { if(((stu+i)->gender) != ‘f‘){ ((stu+i)->gender) = ‘f‘; } printf("%s %d %c %f\n",(stu+i)->name,stu[i].age,(*(stu+i)).gender,(stu+i)->score); } printf("/////////////////////////////////////\n"); //按年龄排序 for (int i = 0; i < n-1; i++) { for (int j =0; j< n-1-i; j++) { if ((stu+j)->age > (stu+j+1)->age) { Student temp = stu[j]; stu[j] = stu[j+1]; stu[j+1] = temp; } } } for (int i =0; i<n; i++) { printf("%s %d %c %f\n",(stu+i)->name,stu[i].age,(*(stu+i)).gender,(stu+i)->score); } printf("/////////////////////////////////////\n"); } int main(int argc, const char * argv[]) { Student stu4[4] = {{"kate",18,‘f‘,65},{"yasuo",30,‘m‘,400},{"zelasi",29,‘?‘,30},{"guafu",23,‘f‘,89}}; Student *stu = stu;//为什么没有取地址符& 因为数组本身是有地址的 stu指向首个成员变量的地址 printStudent(stu4, 4); return 0; }
标签:
原文地址:http://www.cnblogs.com/ChinaTiger/p/4716128.html