开发者

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

开发者 https://www.devze.com 2023-01-18 13:05 出处:网络
I\'m new to C and still trying to grasp the concept of pointers.I know开发者_JAVA技巧 how to write a swap function that works...I\'m more concerned as to why this particular one doesn\'t.

I'm new to C and still trying to grasp the concept of pointers. I know开发者_JAVA技巧 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);
}


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.


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.


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.


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.


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;
}


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. 
}


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.


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);
}


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
}


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.


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


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

Works good.

0

精彩评论

暂无评论...
验证码 换一张
取 消