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.
Basically the output should read:
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.