Adam Soffer
Adam Soffer

Reputation: 1654

Simple swap function...why doesn't this one swap?

I'm new to C and still trying to grasp the concept of pointers. I know how to write a swap function that works...I'm more concerned as to why this particular one doesn't.

void swap(int* a, int* b)
{
 int* temp = a;
 a = b;
 b = temp;
}

int main()
{
 int x = 5, y = 10;
 int *a = &x, *b = &y;
 swap(a, b);
 printf(“%d %d\n”), *a, *b);
}

Upvotes: 10

Views: 37911

Answers (12)

Bogdan Bogdy
Bogdan Bogdy

Reputation: 1

#define SWAP(a,b) ((a)=(b)+(a),(b)=(a)-(b),(a)=(a)-(b))

Works good.

Upvotes: -1

manu kj
manu kj

Reputation: 1

You need to send the address of a and b for swap function so while calling swap function you must call ass swap (&a,&b) So that you pass the address, and alter the address

Upvotes: -1

Hitesh Modha
Hitesh Modha

Reputation: 2790

Without using a third variable (temp)

void swap(int* a,int* b)
{ 
 // a = 10, b = 5;
  *a = *a + *b;  // a now becomes 15
  *b = *a - *b;  // b becomes 10
  *a = *a - *b;  // a becomes 5
}

Upvotes: 1

shuriquen
shuriquen

Reputation: 899

Umm maybe using this

void swap(int** a, int** b)
{
 int** temp = a;
 a = b;
 b = temp;
}

int main()
{
 int x = 5, y = 10;
 int *a = &x, *b = &y;
 swap(&a, &b);
 printf(“%d %d\n”), *a, *b);
}

Upvotes: 1

Ziffusion
Ziffusion

Reputation: 8923

When thinking about pointers, you need to be clear on a few abstractions.

An object in memory. This can be of any type (and size). An integer object, for example, will occupy 4 bytes in memory (on 32 bit machines). A pointer object will occupy 4 bytes in memory (on 32 bit machines). As should be obvious, the integer object holds integer values; a pointer object holds addresses of other objects.

The C programming language lets symbols (variables) represent these objects in memory. When you declare,

int i;

the symbol (variable) i represents some integer object in memory. More specifically, it represents the value of this object. You can manipulate this value by using i in the program.

&i will give you the address of this object in memory.

A pointer object can hold the address of another object. You declare a pointer object by using the syntax,

int* ptr;

Just like other variables, the pointer variable represents the value of an object, a pointer object. This value just happens to be an address of some other object. You set the value of a pointer object like so,

ptr = &i;

Now, when you say ptr in the program, you are referring to its value, which is the address of i. But if you say *ptr, you are referring to not the value of ptr, but rather the value of the object whose address is in ptr i.e. i.

The problem with your swap function is that you are swapping values of pointers, not the values of objects that these pointers hold addresses for. To get to the values of objects, you would have to use *ptr.

Upvotes: 4

Sridhar Iyer
Sridhar Iyer

Reputation: 2840

zildjohn1's answer is the easiest and clearest way to do it. However if you insist on swapping the pointers, then you have to pass the pointer to the pointer because the pointer itself is passed by value.

Upvotes: 0

caf
caf

Reputation: 239011

Your swap() function does work, after a fashion - it swaps the values of the variables a and b that are local to swap(). Unfortunately, those are distinct from the a and b in main() - so you don't actually see any effect from swapping them.

Upvotes: 12

AnT stands with Russia
AnT stands with Russia

Reputation: 320401

It does swap. It swaps local pointers a and b inside swap function. It swaps them perfectly fine, as it should.

If you want to swap the values these pointers are pointing to, you should re-implement your swap function accordingly, i.e. make it swap the pointed values, not the pointers.

Upvotes: 1

zildjohn01
zildjohn01

Reputation: 11515

You're missing *s in the swap function. Try:

void swap(int* a, int* b)
{
 int temp = *a;
 *a = *b;
 *b = temp;
}

That way, instead of just swapping the pointers, you're swapping the ints that the pointers are pointing to.

Upvotes: 30

Carl Norum
Carl Norum

Reputation: 224864

C is a pass-by-value language. Your swap routine doesn't dereference the pointers passed to it, so from main's perspective nothing has happened.

Upvotes: 2

abelenky
abelenky

Reputation: 64682

The right way to do it:

void swap(int* a, int* b)
{
    int temp = *a;  // Temp is set to the value stored at a (5)
    *a = *b;        // value stored at a is changed to the value stored at b (10)
    *b = temp;      // value stored in address b is changed to 5. 
}

Upvotes: 1

Preet Sangha
Preet Sangha

Reputation: 65496

The pointers are passed by value. This means a & b are still a and b when the come back from the function;

try something like this

void swap(int* a, int* b)
{
 int temp = *a;
 *a = *b;
 *b = temp;
}

Upvotes: 1

Related Questions