C語言 如何實現一結構數組指針的內容交換
最佳回答:
#include <stdio.h>
#include <stdlib.h>
typedef struct MyStruct
{
int m, n;
}mystruct;
int main()
{
//初始化
mystruct *a = (mystruct *)malloc(sizeof(mystruct));
mystruct *b = (mystruct *)malloc(sizeof(mystruct));
mystruct t;
a->m = 1, a->n = 5;
b->m = 10, b->n = 50;
//交換
t = *a;
*a = *b;
*b = t;
//顯示結果
printf("%d,%d\n", a->m, a->n);
printf("%d,%d\n", b->m, b->n);
system("pause");
return 0;
}
-
追問:
-
for (i=0;i<MAX_S;i++)
{
s[i]=(st *)malloc(sizeof(st));
memset(s[i],0,sizeof(st));
}
st temp;
temp=*s[n];
*s[n]=*s[m];
*s[m]=temp;
這個是錯在哪裡
-
回答:
-
沒錯
-
追問:
-
雖然,沒解決,但謝謝了