0

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

we can store pointer value to pointer using dereferencing i.e(*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.

4 Answers4

3

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)
abligh
  • 24,573
  • 4
  • 47
  • 84
  • Hi, could you tell me why we need to warp up the function with do-while here? – Unheilig Dec 20 '14 at 12:55
  • @Unheilig - semicolon swallowing. See http://stackoverflow.com/questions/154136/do-while-and-if-else-statements-in-c-c-macros – abligh Dec 20 '14 at 13:23
2

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • i am not expecting output...when we call a function with parameters it copy the variable why 2nd function is expecting address?? –  Dec 20 '14 at 12:19
  • @goutham Please check the data types in that case. `&a` is `int *`, while `*x` is `int`. Your function expects `int *`, not `int`s. – Sourav Ghosh Dec 20 '14 at 12:20
  • sorry to ask am not getting how you are saying int *x and *y are type of int and int *p in function is type of int * –  Dec 20 '14 at 12:38
  • @goutham :-) Nothing to be sorry. I understand the problem. Don't get confused. **I _never_ said**, `int *x` is `int`. **I said** , `*x` is `int`, `x` is `int*`. Same way, `p` is `int *`, `*p` is `int`. Clear now? – Sourav Ghosh Dec 20 '14 at 12:42
1

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.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
0

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

Oswald
  • 31,254
  • 3
  • 43
  • 68
  • sorry to ask am not getting how you are saying int *x and *y are type of int and int *p in function is type of int * –  Dec 20 '14 at 12:35
  • Each expression yields a value. That value has a type. A variable is an expression. That means, in your code `x` is an expression. Its type is *pointer to `int`*. `*x` is also an expression. Its type is `int`. Your swap function expects arguments of type *pointer to `int`*. When you pass `*x` to that function, you are passing a value of type `int`, not *pointer to `int`*. – Oswald Dec 20 '14 at 12:45