指針練習:SwapMemory
阿新 • • 發佈:2017-09-17
ace col tint ref nta tmp 0ms statistic itl
指針練習:SwapMemory
- 查看
- 提交
- 統計
- 提問
- 總時間限制:
- 1000ms
- 內存限制:
- 65536kB
- 描述
-
填寫內存交換函數 SwapMemory,使得程序輸出指定結果
-
#include <iostream> using namespace std; void SwapMemory(void * m1,void * m2, int size) {
// 在此處補充你的代碼
-
} void PrintIntArray(int * a,int n) { for(int i = 0;i < n; ++i) cout << a[i] << ","; cout << endl; } int main() { int a[5] = {1,2,3,4,5}; int b[5] = {10,20,30,40,50}; SwapMemory(a,b,5 * sizeof(int)); PrintIntArray(a,5); PrintIntArray(b,5); char s1[] = "12345"; char s2[] = "abcde"; SwapMemory(s1,s2,5); cout << s1 << endl; cout << s2 << endl; return 0; }
- 輸入
- 無
- 輸出
- 10,20,30,40,50,
1,2,3,4,5,
abcde
12345 - 樣例輸入
-
無
- 樣例輸出
-
10,20,30,40,50, 1,2,3,4,5, abcde 12345
- 來源
- Guo Wei
- 源代碼:
-
char* p1 = (char*)m1;
char* p2 = (char*)m2;
for(int i=0; i<size; i++) {
char tmp = p1[i];
p1[i] = p2[i];
p2[i] = tmp;
}
指針練習:SwapMemory