I'd like some help with making a swap function.

• 02-01-2011
sw881118
I'd like some help with making a swap function.
Hello,

I am a student learning to program in C and currently learning about pointers.

I am having trouble completing a program that swaps and prints two arrays.

10, 9, 8, 7, 6, 5, 4, 3, 2, 1
1, 2, 3, 4, 5, 6, 7, 8, 9, 10

I have setup a function called 'swap' where the swapping of the data gets done, and printing of the swap function should be in the main function.

Please find the additional details of the restriction in the code below.

Code:

``` // Cannot use [] in the swap function. // need to reverse the output of the ar[10] and ar2[10] #include <stdio.h> void swap(int *ptr, int *ptr2); int main(void) {         int ar[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};                // Declaration of the arrays.         int ar2[10]={10, 9, 8, 7, 6, 5, 4, 3, 2, 1};                int i;                 swap(ar, ar2);                                                                        // Call on the swap function.                 /*         printf("Original ar[10]: ");         for(i=0; i<10; i++)                                                                // Printing of the original statement.         {                 printf("%d", ar[i]);         }                 printf("\n\n");                 printf("Original ar2[10]: ");         for(i=0; i<10; i++)                                                                // Printing of the original statement.         {                 printf("%d", ar2[i]);         }                 printf("\n\n");         */                         return 0; } void swap(int *ptr, int *ptr2) {         int temp, i;                 temp=*ptr;         *ptr=*ptr2;         *ptr2=temp;                         return 0; }```
Thanks for all the help in advance.
• 02-01-2011
tabstop
Your swap function swaps a single pair of numbers. You could perhaps use a loop to run each index 0 through 9 into your swap function.
• 02-01-2011
CommonTater
Temp in your swap function should be a pointer to an int... int *temp ... not an int.
• 02-01-2011
tabstop
Quote:

Originally Posted by CommonTater
Temp in your swap function should be a pointer to an int... int *temp ... not an int.

Why would you want it to be a pointer? You are swapping ints.
• 02-01-2011
CommonTater
You will also need to put your swap function into your loops... so it runs across the entire array not just the first item.
• 02-01-2011
itsme86
Quote:

Originally Posted by CommonTater
Temp in your swap function should be a pointer to an int... int *temp ... not an int.

No, it should be an int. If you store the address of the variable you're swapping, then the original value is lost before you can retrieve it to complete the swap.
• 02-01-2011
CommonTater
Quote:

Originally Posted by itsme86
No, it should be an int. If you store the address of the variable you're swapping, then the original value is lost before you can retrieve it to complete the swap.

True... I thought from a quick glance he was swapping pointers, not values... my mistake.
• 02-02-2011
sw881118
Thanks guys for helping me out here.

When I posted the question, I should have probably said that I was aware of putting the loop somewhere in the swap function.

I have tried to re-build the code once more but weirdly I am still very much confused about this problem.

I think I need to go back and study arrays and look at pointers once more.

In the meantime, I would love to get my head around the problem above, can someone help me out to finish the code? (maybe less vague details on completing the code?)

Thanks in advance and I appreciate all the help.
• 02-02-2011
tabstop
Quote:

Originally Posted by sw881118
Thanks guys for helping me out here.

When I posted the question, I should have probably said that I was aware of putting the loop somewhere in the swap function.

I have tried to re-build the code once more but weirdly I am still very much confused about this problem.

I think I need to go back and study arrays and look at pointers once more.

In the meantime, I would love to get my head around the problem above, can someone help me out to finish the code? (maybe less vague details on completing the code?)

Thanks in advance and I appreciate all the help.

I don't really know how much more less vague we can possibly be other than "put your swap statement in a loop". The only other issue here is the fact that you have to pass pointers/addresses to your swap function; so when you want to swap ar[5] with ar2[5], you need to pass ar+5 and ar2+5 to your function (because those are the addresses of ar[5] and ar2[5] respectively).
• 02-02-2011
CommonTater
Quote:

Originally Posted by sw881118
Thanks guys for helping me out here.

When I posted the question, I should have probably said that I was aware of putting the loop somewhere in the swap function.

I have tried to re-build the code once more but weirdly I am still very much confused about this problem.

I think I need to go back and study arrays and look at pointers once more.

In the meantime, I would love to get my head around the problem above, can someone help me out to finish the code? (maybe less vague details on completing the code?)

Thanks in advance and I appreciate all the help.

Well, if the goal is to swap the contents of array #1 with those of array #2, it's really a pretty simple loop... use your current swap function, pass in pointers to the array elements swap(&array1[i], &array2[i]) and let it work it's magic.