I have an array of complex numbers, and I just want to swap between two elements. However, I want to implement the swap for unknown types (using void*). So I wrote the following code, using the first swap implementation that I saw here:
#include<stdio.h>
typedef struct complex complex;
struct complex {
int real;
int img;
};
void swap(void *arr[], int i, int j)
{
void *temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void printComplexArray(complex* cArr, size_t len)
{
int i;
for (i = 0; i < len; i++)
{
printf("Cell %d : real- %d , img- %d
", i, cArr[i].real, cArr[i].img);
}
}
void main(void)
{
complex cArr[] = { { 22, 3 },{ 1, 13 },{ 5, 7 },{ 8, 4 } };
size_t cLen = (sizeof(cArr) / sizeof(cArr[0]));
swap(cArr, 1, 2);
printComplexArray(cArr, cLen);
}
I expected to get:
Cell 0 : real- 22 , img- 3
Cell 1 : real- 5 , img- 7
Cell 2 : real- 1 , img- 13
Cell 3 : real- 8 , img- 4
But I got:
Cell 0 : real- 22 , img- 1
Cell 1 : real- 3 , img- 13
Cell 2 : real- 5 , img- 7
Cell 3 : real- 8 , img- 4
As you can see, not only that the swapping wasn't performed correctly- the content of some array elements was changed. Can you explain why, and how can I fix this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…