6

I am supposed to use pointers to swap ints in an array. It compiles with no errors or warnings and runs but does not swap the ints. Any suggestions would be helpful!!!

Here is the tester:

#import <stdio.h>

void swap( int ary[] );

int main(  int argc, char*argv[] )
{
    int ary[] = { 25, 50 };
    printf( "The array values are: %i and %i \n", ary[0], ary[1] );
    swap( ary );
    printf( "After swaping the values are: %i and %i \n", ary[0], ary[1] );

    return 0;
}

Here is the swap function:

void swap( int ary[] )
{
    int temp = *ary;
    *ary = *(ary + 1);
    *ary = temp;
}

This is what is displayed after running:

The array values are: 25 and 50
After swaping the values are: 25 and 50

7 Answers 7

21

I hate spoiling this but it looks like a typo more than anything.

In your swap function:

*ary = temp;

should be:

*(ary + 1) = temp;

edit: Is there a reason you're not using array notation? I think it's a bit clearer for things like this:

int temp = ary[0];
ary[0] = ary[1];
ary[1] = temp;
Sign up to request clarification or add additional context in comments.

6 Comments

I think we've all been there.
Indeed, use [], it's much cleaner.
It should also be include, not import.
@paxdiablo: Perhaps he is running in XCode and is actually compiling as Objective-C (.m suffix insteaD of .c)?
@Josh - Why? Both are mathematically and machine-code-ally equivalent.
|
6

Examine your swap function more carefully:

void swap( int ary[] )
{
    int temp = *ary;
    *ary = *(ary + 1);
    *ary = temp;
}

When does *(ary + 1) get assigned to?

Comments

4

You move the second value into the first spot, and then move the first value back into the first spot.

Comments

4

just for fun; It's also possible to swap without using a temporary value

void swap( int ary[] )
{
    *ary ^= *(ary + 1);
    *(ary + 1) ^= *ary;
    *ary ^= *(ary + 1);
}

As GMan points out, this code obscures your intent from the compiler and the processor, so the performance may be worse than using a temp variable, especially on a modern CPU.

4 Comments

What does the '^=' operator do here? Never seen it used before
^ is exclusive or, so x^=y means x=x^y
^ is a bit XOR. 0 ^ 0 = 0 1 ^ 1 = 0 1 ^ 0 = 0 0 ^ 1 = 0
You should mention this probably isn't worth it. Modern CPU's execute instructions in parallel, and this causes a pipe-line stall. Not to mention it's ugly.
2

You can also swap the values without any temporary variable:

void swap(int *x, int *y)
{
   *x ^= *y;
   *y ^= *x;
   *x ^= *y;
}

then call:

swap(&ary[0], &ary[1]);

Comments

0

Try this instead:

void swap( int ary[] )
{
    int temp = ary[0];
    ary[0] = ary[1];
    ary[1] = temp;
}

4 Comments

I know how to do it with array notation but we were required to use pointers.
@Josg i'll open a little secret to you. You can take [] notation, and make the thing, that compilers do: a[x] = *(a + x) = x[a]. YEAH! it's a little c-trick about a[x] = x[a], cause compilers don't use []-method.
i mean you can take the Peter's code and just edit it little bit: arr[1] -> *(arr+1)
What f0b0s is saying (and I agree) is that for all intents and purposes there is no difference between an array name and a pointer (ignoring doing foo++); the only questions are where is the space allocated? (stack, heap, or 'global space'), and how many elements are in the array? Both of these questions also should send up red flags because it means a) you don't know how far you can meaningfully index into the array, and b) you don't know if you can/should deallocate anything. Gack. Give me Python.
0

your swap function will work only for 2-ints array, so show it to your compiler (it won't change anything, but make code cleaner)

void swap( int ary[2] )

1 Comment

-1 this doesn't accomplish anything, and really doesn't help anyone in the long run. Your "clearer code" to try to help us understand your intent is no substitute for decent documentation.

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.