一、多級(jí)指針
#include <stdio.h>
int main(void)
{
int i = 4;
int * p = &i;
int ** q = &p;
int *** r = &q;
printf("***r = %d", ***r);
return 0;
}
二廓俭、動(dòng)態(tài)分配內(nèi)存實(shí)現(xiàn)跨函數(shù)使用內(nèi)存
/*
動(dòng)態(tài)分配內(nèi)存malloc使得跨函數(shù)使用內(nèi)存
*/
#include <stdio.h>
#include <stdlib.h>
void f(int ** q)
{
*q = (int *) malloc(sizeof(int));
**q = 5; // 等價(jià)于*p = 5
// 由于沒(méi)有free(q)云石,所以函數(shù)終止后可以被其他函數(shù)使用
}
int main(void)
{
int * p;
f(&p); // 要想通過(guò)f函數(shù)內(nèi)部修改p的值,只能發(fā)送p的地址研乒,&p是 int **類型
printf("*p = %d\n", *p);
return 0;
}
- 有什么用汹忠? 通過(guò)函數(shù)來(lái)建立某內(nèi)存并返回該首個(gè)地址
phead = creat_list()
三、結(jié)構(gòu)體
3.1 創(chuàng)建結(jié)構(gòu)體及初始化方式
#include <stdio.h>
struct Student // 定義一個(gè)新數(shù)據(jù)類型雹熬;struct可以放main函數(shù)里
{
char name[20];
int age;
}错维; // 切記“;”不能丟
int main(void)
{
struct Student st1 = {"abnc", 12}, st2; // st={}第一種初始化方式
st2.age = 12; // st.name第二種初始化方式
strcpy(st2.name, "Her"); // 切記橄唬,字符串不能用“=”復(fù)制,用strcpy
printf("%s %d %d\n", st1.name, st1.age);
printf("%s %d %d\n", st2.name, st2.age);
return 0;
}
3.2 指向結(jié)構(gòu)體變量的指針
#include <stdio.h>
#include <stdlib.h>
struct Student
{
char name[20];
int age;
};
int main(void)
{
struct Student st = {"abnc", 12};
struct Student * pts = &st1; // 創(chuàng)建指向結(jié)構(gòu)體的指針
pts->age = 12; // 第三種初始化方式
strcpy(pts->name, "Bob");
printf("%s %d\n", st.name, st.age);
return 0;
}
- pst->age等價(jià)于(*pst).age参歹,也就是等價(jià)于st.age仰楚;其含義就是
- pst所指向的結(jié)構(gòu)體變量中的age這個(gè)成員
3.3 通過(guò)函數(shù)來(lái)完成對(duì)結(jié)構(gòu)體變量的輸入和輸出
#include <stdio.h>
#include <stdlib.h>
struct Student
{
char name[20];
int age;
};
void InputStudent(struct Student * pstu);
void OutputStudent(struct Student * pstu);
int main(void)
{
struct Student st1 = {"abnc", 12};
struct Student * pts = &st1; // 創(chuàng)建指向結(jié)構(gòu)體的指針
InputStudent(pts);
OutputStudent(pts);
return 0;
}
void InputStudent(struct Student * pstu)
{
pstu->age = 100;
strcpy(pstu->name, "Haaaaa");
}
void OutputStudent(const struct Student * pstu) // 這里傳入指針更快,因?yàn)橹羔樦徽?個(gè)字節(jié)!
// 只讀不寫的加個(gè)const來(lái)保護(hù)原變量
{
printf("%d, %s\n", pstu->age, pstu->name);
}
3.4 動(dòng)態(tài)構(gòu)造存放學(xué)生管理信息系統(tǒng)(結(jié)構(gòu)體數(shù)組)
- 信息系統(tǒng)要求
- 用戶輸入學(xué)生名字和分?jǐn)?shù)
- 輸入后對(duì)分?jǐn)?shù)進(jìn)行排序
- 輸出排序后的學(xué)生信息
- 要求以函數(shù)方式寫
#include <stdio.h>
#include <stdlib.h>
struct Student
{
char name[100];
int score;
};
void InputStudent(struct Student * pArr, int len);
void OutputStudent(const struct Student * pArr, int len);
void SortStudent(struct Student * pArr, int len);
int main(void)
{
int len;
struct Student * pArr;
printf("請(qǐng)輸入學(xué)生的個(gè)數(shù):\n");
scanf("%d", &len);
pArr = (struct Student *)malloc(len * sizeof(struct Student));
InputStudent(pArr, len); // 傳指針僧界,類似于傳數(shù)組
SortStudent(pArr, len);
OutputStudent(pArr, len);
return 0;
}
void InputStudent(struct Student * pArr, int len) // 接收指針侨嘀,類似于接數(shù)組
{
int i;
for(i = 0; i < len; ++i)
{
printf("請(qǐng)輸入第%d個(gè)學(xué)生的信息:\n", i+1);
printf("name = ");
scanf("%s", pArr[i].name); //name是數(shù)組名,本身就已經(jīng)是數(shù)組首元素的地址, 所以pArr[i].name 不能改成 &pArr[i].name
printf("score = ");
scanf("%d", &pArr[i].score); // 指針變量名當(dāng)數(shù)組用
}
}
void SortStudent(struct Student * pArr, int len)
{
int i, j;
struct Student t;
for(i=0; i<len-1; ++i)
{
for(j=0; j<len-1-i; ++j)
{
if(pArr[j].score < pArr[j+1].score)
{
t = pArr[j];
pArr[j] = pArr[j+1];
pArr[j+1] = t;
}
}
}
}
void OutputStudent(const struct Student * pArr, int len)
{
int i;
printf("\n分?jǐn)?shù)排名如下");
for(i = 0; i < len; ++i)
{
printf("\n第%d個(gè)學(xué)生的信息:\n", i+1);
printf("name = %s", pArr[i].name);
printf("score = %d", pArr[i].score);
}
printf("\n");
}
- 總結(jié):
- 用指針做數(shù)組名捂襟,也就是說(shuō)咬腕,malloc后,對(duì)指針取下標(biāo)完全可仍岷伞涨共;傳數(shù)組指針就是傳數(shù)組的含義
- 注意指針就是地址,指針變量存的就是地址
- 對(duì)于冒泡排序宠漩,小于號(hào)就是升序举反;大于號(hào)就是降序
- 對(duì)于為什么scanf中數(shù)組不用“&”:
#include <stdio.h> int main(void) { int a[10]; scanf("%d", a); printf("%d\n", *a); // a等價(jià)于第一個(gè)元素的地址 return 0; }
- &pArr[i].score,點(diǎn)運(yùn)算符優(yōu)先于取地址符