简体   繁体   中英

Swapping two integers using pointers using c

I am trying to swap the value of two integers using pointers, see code below: swapping number using pointer in c:

{
    int a = 10;
    int b = 20;

    swapr(&a, &b);
    printf("a=%d\n", a);
    printf("b=%d\n", b);
    return 0;
}

void swapr(int *x, int *y)  //function
{
    int t;
    t=*x;
    *x=*y;
    *y=t;
}

In the code why is swap(&A, &B); used when *x and *y point to a value not an address

When you say (int *x, int *y) you're just declaring x and y as pointers. In all future usages, when you say x , it means the pointer and when you say *x , it means the value it points to.

In a declaration, the * in the declarator indicates that the object has pointer type. Take the declaration

int *p;

The type of p is "pointer to int ". This type is specified by the combination of the type specifier int and the declarator *p .

Pointer-ness, array-ness, and function-ness are all specified as part of the declarator:

T *p;       // p is a pointer to T
T a[N];     // a is an N-element array of T
T f();      // f is a function returning T
T *ap[N];   // ap is an array of pointers to T
T (*pa)[N]; // pa is a pointer to an array of T
T *fp();    // fp is a function returning pointer to T
T (*pf)();  // pf is a pointer to a function returning T

etc.

In C, declaration mimics use - if you have a pointer to an int named p and you want to access the value it points to, you dereference it with the * operator, like so:

x = *p;

The expression *p has type int , so the declaration of p is

int *p;

In main , the expressions &a and &b have type int * , so the corresponding parameters have to be declared as int * .

 x == &a // int * == int *
 y == &b // int * == int *

*x ==  a // int == int
*y ==  b // int == int

Basically the way var declarations work is if you declare a variable

int c;

You've just declared an integer and you can assign values to it or retrieve its value like this

int a;
int b;
a = 10;  // assign 10
b = a;   //assign value of a to b

Pointers are a bit different though. If you declare a pointer and you want to assign a value to it then you must dereference it with the * operator

int * a;  // declare a pointer
int b;    // declare a var
b = 10;   // assign 10 to b
*a = b;   // assign 10 as the value of a
b = 20;   // b is now 20 but the var a remains 10

But you can also assign a pointer to point at a memory address

int * a;
int b;
b = 10;   // assign 10 to b
a = &b;   // assign address of b to a (a points at b)
b = 20;   // value of b changes (thus value of a is also 20 since it is pointing at b

So if you have a function signature

int func (int * a, int * b);

All this is means is that the function takes the address of two variable

int a;
int b;
int * x;
int * y;

func(&a, &b); // send it the address of a and b
func(x, y);   // send it the address of x and y
func(x, &b);  

Basically a normal var's address can be accessed with the & operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM