交换结构体内部的两组数据

#include<stdio.h>
#define  N 30
typedef struct Mystruct
{
    int a;
    int b;
}MYSTRUCT;
void Swap(struct Mystruct *p,struct Mystruct *p1)
{
//  struct Mystruct *pstr=p1;
    struct Mystruct a;
    a=*p1;
    *p1=*p;
    *p=a;
    printf("%d\n",*p);
    printf("%d\n",*p1);

}
int main()
{
    struct Mystruct *pstr;
    struct Mystruct *pstr1; 
    MYSTRUCT str[N];
    str[0].a=1;
    str[0].b=3;
    str[1].a=2;
    str[1].b=4;
    pstr=&str[0];
    pstr1=&str[1];

    printf("%d\n",*pstr);
    printf("%d\n",*pstr1);

    Swap(pstr,pstr1);

    printf("%d\n",str[0]);
    printf("%d\n",str[1]);

    printf("%d\n",str[0].b);
    printf("%d\n",str[1].b);
}

猜你喜欢

转载自blog.csdn.net/ywdndm/article/details/78980051