简体   繁体   中英

why can't we pass dereferencing pointer to pointer?

i am just trying different was to pass pointer,i came across this doubt.

we can store pointer value to pointer using dereferencing ie(*p=*q) why not we can pass through functions.

void swap(int *p,int *q)
{
int temp=*p;
*p=*q; /** when this is possible**/
*q=temp;
}



int main()
{
 int a=5,b=10;
 int *x,*y;
 x=&a;
 y=&b;
 swap(&a,&b);
 swap(x,y);
 swap(*x,*y);/*why this is not possibel*/

}

   *p=*q when this is possible.

1. swap(&a,&b); This is possible 2. swap(*x,*y); This is not possible. Why?

Can anyone post good material on pointers in depth.

Your second example should read

swap(x,y);

As x=&a and y=&b already.

If you wanted to swap the pointers over (rather than their content) you would need something like:

void swapptr(int **p,int **q)
{
    int temp=*p;
    *p=*q;
    *q=temp;
}

Note the different definitions of the function arguments.

If you want to swap generically, you will either need a macro that passes the type, like this:

#define SWAP(a, b, type) do { type _temp = a; a = b; b = _temp; } while (0)

or to use a gcc extension, like this:

#define SWAP(a, b) do { typeof(a) _temp = a; a = b; b = _temp; } while (0)

See, &a is equivalent to x in your case.

Why do you want *x ? Isn't passing x is simmilar to passing &a ?

The data type of *x is int , the data type of x is int * . Your function is expecting arguments to be passed of type int * .

Maybe its worthy to mention [in context of your code]

void func (int p,int q) should be called as func(a,b) .

That does not imply , void func(int *p,int *q) should be called as func(*a,*b) , rather it should be called as func(&a, &b);

Simply use swap (x, y) .

As a sidenote: C uses pass by value method. If you're passing the values as function arguments, inside the function, it will be received in the variables which are in local scope to the function and any changes made to them will not be reflected in the actual arguments.

That's why, you need to pass the address of the variables , not the values itself.

The second one is not possible because in that case you will be using pass by value and not the pointer,and hence, the values in the called function would be changed,but,not reflected in the main method as they were local to that method and you didn't use pointers.

Another possible solution to achieve the solution is,for that you need to change your function call in main . The function call would be changed to

swap(x,y);  //don't use dereferenced pointers here as it will result in call by value.

void swap(int *p,int *q)
{
int temp=*p;
*p=*q;  (** when this is possible**)
*q=temp;
}

This is done.

After int *x,*y; the type of *x and *y are int . Your swap function expects two int* as arguments, not two int .

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