0

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

Kelly
  • 213
  • 1
  • 6
  • 21
  • 3
    Why aren't you using a temporary variable? Honestly, I'd be willing to bet that the compiler can optimize `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. – Chris Lutz Aug 10 '11 at 07:37
  • Thank Chris. Using a temporary would solve. But is there any way we can do this without using temp & without having to pass array length? – Kelly Aug 10 '11 at 07:43
  • 1
    It's probably more convenient to use pointers, and simply swap the pointers, rather than copying the entire contents of two arrays. – Sander De Dycker Aug 10 '11 at 07:52
  • 1
    @Kelly - Please answer my question: _Why_ do you want to avoid a temporary variable? (And no, there's no way to avoid passing array lengths to a function. Also, your `while` condition will not work, and also seems to indicate a misunderstanding of operator precedence: `x && y == z` will not test whether `x` and `y` are both equal to `z`. For that you need `x == z && y == z`. But using `array[idx] == NULL` won't work, because `array[idx]` is an `int` and `NULL` is a pointer. You need to pass the array length, and do `idx < len`. – Chris Lutz Aug 10 '11 at 08:02
  • @Chris, you are right about everything you said. I was just wondering whether it would be possible to do so. – Kelly Aug 10 '11 at 10:09

4 Answers4

3

The while condition is wrong and you can type less.

for (index = 0; index < len; index++) {
    array1[index] ^= array2[index];
    array2[index] ^= array1[index];
    array1[index] ^= array2[index];
}

Or you can use a temporary variable as indicated by this C FAQ.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
1

array2[index] != NULL is wrong - it's not a pointer at all, and you are comparing it against a pointer value. Nor is array1[index] correct as a test - it can only be false if the array contains a zero at some position, otherwise you are dealing with undefined behaviour once you go past the allocated area.

You should pass the length of the arrays to the function and then the condition of the while loop should be index < length.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
  • Thanks Blagovest. I missed that part. But is there a way we can do this without passing the length of the array ? – Kelly Aug 10 '11 at 07:37
  • @Kelly - No. In C you must pass the array lengths. You can only pass one length and tell users of your code it's undefined behavior to use arrays that aren't the same length, but there's no way to just "know" the length of an array that's been passed to a function. – Chris Lutz Aug 10 '11 at 07:42
  • @Kelly: take a look at this approach: http://stackoverflow.com/questions/6966570/why-declare-a-struct-that-only-contains-an-array-in-c – Blagovest Buyukliev Aug 10 '11 at 07:42
  • 1
    @Blagovest - Sure, but then your code only works for arrays of a fixed size, and you have to declare all of your arrays as the special `struct` type and you end up with lots of potential wasted space. Also, you're really just hiding the underlying `memcpy` that's going to be called to copy the `struct`s, and you'll still need a temporary variable. – Chris Lutz Aug 10 '11 at 07:59
1

correct your while condition and you can use the while loop

index = len;
while( index-- ) {
    array1[index] ^= array2[index];
    array2[index] ^= array1[index];
    array1[index] ^= array2[index];
}

or use your length info directly

while( len-- ) {
    array1[len] ^= array2[len];
    array2[len] ^= array1[len];
    array1[len] ^= array2[len];
}
user411313
  • 3,930
  • 19
  • 16
1

Just change condition like that,

index =0;
while(array1[index] != NULL  && array2[index] != NULL)
{
    array1[index] ^= array2[index];
    array1[index] ^= array2[index];
    array1[index] ^= array2[index]; 
    index++;
}
viral
  • 4,168
  • 5
  • 43
  • 68