locked
pointer argument RRS feed

  • Question

  • #include <iostream>
    using namespace std;
    void swap(int *x, int *y);
    void neg (int *x);

    int main(void)
    {
      int  a, b, d;
      a = 10;
      b = 20;
      d = 30;
      swap(a, b);
      neg(d);
      system("pause");

      return 0;
    }
    void swap(int *x, int *y)
    {
      int temp;

      temp = *x;  
      *x = *y;  
      *y = temp;  
    }

    void neg (int *x){
    int temp;
    temp = -*x;
    *x = temp;}

    Question: Both swap and neg functions need  pointer type arguments.  If I send two integers(a, b) to swap it works.  But if I send an integer(d)

                     to neg I get compilation error. Why ?

    Monday, December 3, 2012 9:46 AM