嗨,我想創建一個交換函數,交換結構的前兩個元素。有人可以告訴我如何使這項工作。交換c中的兩個結構
void swap(struct StudentRecord *A, struct StudentRecord *B){
struct StudentRecord *temp = *A;
*A = *B;
*B = *temp;
}
struct StudentRecord *pSRecord[numrecords];
for(int i = 0; i < numrecords; i++) {
pSRecord[i] = &SRecords[i];
}
printf("%p \n", pSRecord[0]);
printf("%p \n", pSRecord[1]);
swap(&pSRecord[0], &pSRecord[1]);
printf("%p \n", pSRecord[0]);
printf("%p \n", pSRecord[1]);
最好讓'temp'成爲一個值,而不是指針。 –
'struct StudentRecord * temp = * A;' - >'struct StudentRecord temp = * A;'''* B = * temp;' - >'* B = temp;'.....'swap (pSRecord [0],&pSRecord [1]);' - >'交換(pSRecord [0],pSRecord [1]);'或'交換(&SRecords [0],&SRecords [1]);' – BLUEPIXY
一旁:並不是每個人都厭倦了包含「學生」的C代碼。那麼所有的老師都會在全世界做同樣的任務嗎? –