2

Consider the following C program:

#include <stdio.h>

void swap(int []); 

int main(void) {
     int a[3] = {1, 2, 3};
     swap(a);
     printf("%d %d %d\n", a[0], a[1], a[2]);
     swap(&a[1]);
     printf("%d %d %d\n", a[0], a[1], a[2]);
     return 0; 
}

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

Can anyone explain why the following code gives an output
2 1 3
2 3 1

rather than
2 1 3
1 2 3

I understand that swap(a) swaps a[0] and a[1]. But I'm not so sure how swap(&a[1]) works. In both cases we are passing the array a into the function swap, are we not? So my hypothesis was that swap(&a[1]) should again swap a[0] and a[1], giving us back the original order 2 3 1.

EDIT: This code was written as intended. I just wanted to see what happens if we pass an address of an element other than the first element into the function. Apparently if I pass &a[n] into the function, it disregards all elements before a[n] and treats a[n] as the first element?

2
  • Yes, I understand that. But how exactly does swap(&a[1]) work? In other words, what is going on when we pass into the function an address that is not the first element? Does it then treat &a[1] as the address of the first element? Commented Nov 25, 2014 at 4:47
  • change swap(&a[1]) to swap(&a[0]) Commented Nov 25, 2014 at 4:49

4 Answers 4

1

Your swap function takes and address of an integer as input and swaps element at that address with element at next address.

swap(a) is same as swap(&a[0])
It will swap element at index 0 with element at index 1.
Now your array becomes {2,1,3}
swap(&a[1]) swaps element at index 1 with element at index 2.
So your array now becomes {2,3,1}

Remember when you pass an array actually you are passing address of element at index 0 (&a[0]).

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

5 Comments

So when I pass &a[1] into the function, it treats &a[1] as though it was &a[0]? Because in the function definition of swap, it clearly indicates swapping the elements at index 0 and 1.
@Vizuna: When you pass an array to a function it decays to a pointer. Your function signature is the same as saying void swap(int *x). So the function assumes it has a pointer to at least two consecutive int and swaps the values at *(x+0) and *(x+1).
Yes, when you pass &a[1] x[0] is same as a[1]. Means your x (array in swap function) starts from index 1 of a (array in your main)
@Blastfurnace I see. So if I pass the pointer into the function, it treats the element pointed to as the first element, regardless of its position in the original array?
@Vizuna: Yes, you must remember that the function doesn't actually see an array type, just a pointer. That's also why you can't determine the "size" of an array parameter, it's already decayed to a pointer.
0

Because when you swap first time you sent an array starting with 0 index then it exchanges 0th and 1st index values means at first swap, array becomes {2,1,3} then again you sent this array starting with 1 index means you sent only two values {1,3}, then that values gets exchanges. In this case x[0] = 1 & x[1] = 3, that is why the output came as {2,3,1} here 2 remains as it is.

Comments

0

In the first call swap(a); you are passing the entire array (actually a pointer to the first element.) So you are swapping elements 0 and 1, which are 1 and 2, respectively, therefore your first output is 2 1 3.

In the second call swap(&a[1]); you are passing the address of the second element in the array, and in your swap function you are therefore swapping the second and third elements, at indices 1 and 2, which are 1 and 3, respectively. That's why your answer after the second iteration is 2 3 1.

Possibly what you meant was

swap(&a[0]);

This will swap the values at indices 0 and 1, which will give you the desired output of 1 2 3.

Comments

0

In your code before the first println statement you are passing a complete array to the parameter of the fuction but in the next you are just passing a single value of location of a[1] which make bit confusing for the function because it has only a single value to swap or it might be as first you passed a zero(0) index or the array to swap with the next time you pass first 1 index of array to swap with which means [1,3 to swap] so pass complete in next statment as this

 swap(a);
 printf("%d %d %d\n", a[0], a[1], a[2]);
 swap(a);
 printf("%d %d %d\n", a[0], a[1], a[2]);
 return 0;

this will generate your desire answer

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.