i have two array's say int array1[6] = { 2, 4, 5, 7, 9 }; & int array2[6] = {0 ,5 ,6 , 7, 3}
I will pass these to a function swap(array1,array2)
I am currently trying to do it as below
index =0;
while(array1[index] && array2[index] != NULL)
{
array1[index] = array1[index] ^ array2[index];
array2[index] = array1[index] ^ array2[index];
array1[index] = array1[index] ^ array2[index];
index++;
}
Is my approach correct? Please let me know your views
PS: I cannot send in array length as a parameter to the function. I would like to do this in C language.
Thanks
int array3[6]; memcpy(array3, array1, sizeof array1); memcpy(array1, array2, sizeof array1); memcpy(array2, array3, sizeof array1);to be faster than your code. Write it in the way that you find clearest, then optimize if you find it to be a performance problem.whilecondition will not work, and also seems to indicate a misunderstanding of operator precedence:x && y == zwill not test whetherxandyare both equal toz. For that you needx == z && y == z. But usingarray[idx] == NULLwon't work, becausearray[idx]is anintandNULLis a pointer. You need to pass the array length, and doidx < len.