Ampersand and Pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • computernewbie
    New Member
    • Jun 2007
    • 1

    Ampersand and Pointer

    Hello,

    I want to know the difference between the following two?

    void some_func(int &a);

    void some_func(int *a);

    Thank you.
  • svlsr2000
    Recognized Expert New Member
    • Feb 2007
    • 181

    #2
    Originally posted by computernewbie
    Hello,

    I want to know the difference between the following two?

    void some_func(int &a);

    void some_func(int *a);

    Thank you.
    In first a is declared as alias. Latter in function u cannot assign a to some other value

    In some_func(int *a) a is pointer to integer. if u want u can assign a to some other value

    a = &b; is allowed here

    Comment

    • vimalankvk80
      New Member
      • Jun 2007
      • 9

      #3
      int &a; // its a alias.

      int *b; its a pointer ..


      example :

      case 1: // referance..

      int b = 10;

      int &a = b;

      b = 15;

      a = 16;

      cout << a << b; \\ both have the same value a= b =16.

      this is known as alias...

      func( int &a, int &b)
      {

      a++;
      b ++;
      }

      main()
      {

      int a = 1;
      int b = 2;

      func(a,b);


      cout << a << b; // o/p 2 3
      }

      case 2: // pointer

      in this case

      int a = 10;

      int *b = &a; // this take means, b as the address of variable a, to get the value of a, you have to use dereferencing operator.

      example:

      func( int *a, int *b)
      {

      *a++; //a ++ , if you do this the address will be incremented., so use dereferencing operator (*)
      *b ++;
      }

      main()
      {

      int a = 1;
      int b = 2;

      func(&a,&b);


      cout << a << b; // o/p 2 3
      }

      ----------

      so comparing to pointers reference variable is more easy.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Please use code tags in your postings.

        Comment

        Working...