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

5
  • 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. Commented Aug 10, 2011 at 7: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? Commented Aug 10, 2011 at 7:43
  • 1
    It's probably more convenient to use pointers, and simply swap the pointers, rather than copying the entire contents of two arrays. Commented Aug 10, 2011 at 7: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. Commented Aug 10, 2011 at 8:02
  • @Chris, you are right about everything you said. I was just wondering whether it would be possible to do so. Commented Aug 10, 2011 at 10:09

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

Thanks Blagovest. I missed that part. But is there a way we can do this without passing the length of the array ?
@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.
@Kelly: take a look at this approach: stackoverflow.com/questions/6966570/…
@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 structs, and you'll still need a temporary variable.
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];
}

Comments

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++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.