簡體   English   中英

使用指針交換兩個數組

[英]Swapping two arrays using pointer

我想知道為什么下面的代碼不起作用。 我已經使用指針將數組從一個復制到另一個,但是它根本不復制。 我想念什么嗎?

#include <stdio.h> 
typedef struct student {
    int id;
    char *pname;
    double points;
} STUD;

void stud_printx(STUD s) {
    printf("[%d:%s] = %lf\n", s.id, s.pname, s.points);
}

void stud_swap(STUD *s1, STUD *s2) { // space to be filled - my code written
STUD tmp;
     tmp = *s1;
     *s1 = *s2; 
     *s1 = tmp;    


}

int main(void) {
    STUD s1 = {1, "Choi", 9.9};
    STUD s2 = {2, "Park", 0.1};

    stud_printx(s1);
    stud_printx(s2);

    stud_swap(&s1, &s2 ); // space to be filled  - my code written 

    stud_printx(s1);
    stud_printx(s2);

    return 0;
}
*s1 = *s2;  // Copy original *s2 into *s1
*s1 = tmp;  // Copy original *s1 into *s1

應該

*s1 = *s2;  // Copy original *s2 into *s1
*s2 = tmp;  // Copy original *s1 into *s2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM