一、結構體數組指針定義與使用
//定義結構體
struct User {
int age;
char name[10];
};
int main() {
//定義結構體數組
User users[3] = {{18, "Alice"}, {20, "Bob"}, {22, "Chris"}};
//定義結構體數組指針
User *pUser = users;
//使用結構體數組指針訪問結構體數組
printf("%d %s\n", pUser->age, pUser->name); //18 Alice
printf("%d %s\n", (pUser+1)->age, (pUser+1)->name); //20 Bob
printf("%d %s\n", (pUser+2)->age, (pUser+2)->name); //22 Chris
return 0;
}
結構體數組指針是指向結構體數組首元素的指針,可以通過指針訪問結構體數組中的元素。結構體數組和結構體數組指針在定義和使用上沒有太大差異,只需注意指針的類型為結構體類型的指針。
二、結構體數組指針強類型轉換
//定義結構體
struct User {
int age;
char name[10];
};
int main() {
//定義結構體數組
User users[3] = {{18, "Alice"}, {20, "Bob"}, {22, "Chris"}};
//定義結構體數組指針
void *pVoid = users;
//使用強類型轉換的結構體數組指針訪問結構體數組
printf("%d %s\n", ((User*)pVoid)->age, ((User*)pVoid)->name); //18 Alice
printf("%d %s\n", ((User*)pVoid+1)->age, ((User*)pVoid+1)->name); //20 Bob
printf("%d %s\n", ((User*)pVoid+2)->age, ((User*)pVoid+2)->name); //22 Chris
return 0;
}
結構體數組指針可以強制轉換為任意類型的指針,在訪問時需要進行強類型轉換。如上例,將結構體數組指針強制轉換為結構體類型指針,即可訪問結構體數組。
三、結構體數組指針越界
//定義結構體
struct User {
int age;
char name[10];
};
int main() {
//定義結構體數組
User users[3] = {{18, "Alice"}, {20, "Bob"}, {22, "Chris"}};
//定義結構體數組指針
User *pUser = users;
//越界訪問結構體數組
printf("%d %s\n", (pUser+3)->age, (pUser+3)->name);
return 0;
}
結構體數組指針訪問結構體數組時,需要注意不要越界,否則會訪問到未知的內存區域,導致程序崩潰。
四、結構體數組指針做函數參數
//定義結構體
struct User {
int age;
char name[10];
};
//結構體數組指針做函數參數
void printUser(User *pUser, int n) {
for(int i=0; iage, (pUser+i)->name);
}
}
int main() {
//定義結構體數組
User users[3] = {{18, "Alice"}, {20, "Bob"}, {22, "Chris"}};
//定義結構體數組指針
User *pUser = users;
//傳遞結構體數組指針給函數
printUser(pUser, 3);
return 0;
}
結構體數組指針可以作為函數參數,方便函數對結構體數組進行操作。
五、結構體數組指針如何用
//定義結構體
struct User {
int age;
char name[10];
};
int main() {
//定義結構體數組
User users[3] = {{18, "Alice"}, {20, "Bob"}, {22, "Chris"}};
//定義結構體數組指針
User *pUser = users;
//使用結構體數組指針訪問結構體數組
printf("%d %s\n", pUser->age, pUser->name); //18 Alice
printf("%d %s\n", (pUser+1)->age, (pUser+1)->name); //20 Bob
printf("%d %s\n", (pUser+2)->age, (pUser+2)->name); //22 Chris
//使用結構體數組指針做函數參數
void printUser(User *pUser, int n) {
for(int i=0; iage, (pUser+i)->name);
}
}
printUser(pUser, 3);
//使用結構體數組指針排序
void sortUser(User *pUser, int n) {
for(int i=0; i<n-1; i++) {
for(int j=i+1; jage > (pUser+j)->age) {
User temp = *(pUser+i);
*(pUser+i) = *(pUser+j);
*(pUser+j) = temp;
}
}
}
}
sortUser(pUser, 3);
printUser(pUser, 3);
return 0;
}
結構體數組指針在編程中常用於對結構體數組的操作,如訪問、排序等。
六、結構體數組指針使用
//定義結構體
struct User {
int age;
char name[10];
};
int main() {
//定義結構體數組
User users[3] = {{18, "Alice"}, {20, "Bob"}, {22, "Chris"}};
//定義結構體數組指針
User *pUser = users;
//使用結構體數組指針訪問結構體數組
printf("%d %s\n", pUser->age, pUser->name); //18 Alice
printf("%d %s\n", (pUser+1)->age, (pUser+1)->name); //20 Bob
printf("%d %s\n", (pUser+2)->age, (pUser+2)->name); //22 Chris
//使用結構體數組指針做函數參數
void printUser(User *pUser, int n) {
for(int i=0; iage, (pUser+i)->name);
}
}
printUser(pUser, 3);
//使用結構體數組指針排序
void sortUser(User *pUser, int n) {
for(int i=0; i<n-1; i++) {
for(int j=i+1; jage > (pUser+j)->age) {
User temp = *(pUser+i);
*(pUser+i) = *(pUser+j);
*(pUser+j) = temp;
}
}
}
}
sortUser(pUser, 3);
printUser(pUser, 3);
//使用結構體數組指針賦值
User *pUser1 = pUser; //pUser1指向users數組的首元素
for(int i=0; iage, (pUser1+i)->name);
}
//使用結構體數組指針初始化
User *pUser2 = (User*)malloc(3*sizeof(User)); //動態分配內存
for(int i=0; iage = 18+i;
strcpy((pUser2+i)->name, "Tom");
}
for(int i=0; iage, (pUser2+i)->name);
}
free(pUser2); //釋放內存
return 0;
}
結構體數組指針的使用包括訪問、函數參數、排序、賦值、初始化等,可以根據實際需求靈活運用。
七、結構體數組指針賦值
結構體數組指針可以通過賦值操作指向另一個結構體數組。
八、結構體數組指針排序
結構體數組指針可以通過排序算法對結構體數組進行排序。
九、結構體數組指針初始化
結構體數組指針可以通過動態分配內存進行初始化。
十、結構體數組指針做形參選取
結構體數組指針作為函數形參時,可以根據具體需求選擇傳遞結構體數組還是指向結構體數組的指針。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/245498.html