views:

500

answers:

7

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
+4  A: 

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?

Charles Bailey
+9  A: 

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;
Ron Warholic
wow I feel stupid now!
Josh Curren
I think we've all been there.
Ron Warholic
Indeed, use `[]`, it's much cleaner.
GMan
It should also be include, not import.
paxdiablo
Since it is homework we were required to use pointers.
Josh Curren
@paxdiablo: Perhaps he is running in XCode and is actually compiling as Objective-C (.m suffix insteaD of .c)?
Ed Swangren
@Josh - Why? Both are mathematically and machine-code-ally equivalent.
Chris Lutz
@Chris: Probably to illustrate how arrays can decay into pointers; it helps reinforce the relationship between different methods of indirect access in C/C++.
Ron Warholic
+3  A: 

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

theycallmemorty
A: 

Try this instead:

void swap( int ary[] )
{
    int temp = ary[0];
    ary[0] = ary[1];
    ary[1] = temp;
}
Peter Rowell
I know how to do it with array notation but we were required to use pointers.
Josh Curren
@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.
f0b0s
i mean you can take the Peter's code and just edit it little bit: arr[1] -> *(arr+1)
f0b0s
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.
Peter Rowell
A: 

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.

gnibbler
What does the '^=' operator do here? Never seen it used before
Chris T
`^` is exclusive or, so `x^=y` means `x=x^y`
gnibbler
^ is a bit XOR.0 ^ 0 = 01 ^ 1 = 01 ^ 0 = 00 ^ 1 = 0
f0b0s
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.
GMan
A: 

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] )
f0b0s
-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.
Chris Lutz
A: 

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]);
pozdynchev