嗨,我想创建一个交换函数,交换结构的前两个元素。有人可以告诉我如何使这项工作。交换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代码。那么所有的老师都会在全世界做同样的任务吗? –