12

Ref

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. float &index( float *v, int i )
  6. {
  7.     return v[ i ];
  8. }
  9.  
  10. int main() {
  11.     int x = 33;
  12.     int &y = x;
  13.  
  14.  
  15.     cout << "x (0x" << &x << ") = " << x << endl;
  16.     cout << "y (0x" << &y << ") = " << y << endl;
  17.  
  18.     y = 99;
  19.  
  20.     cout << "y (0x" << &y << ") = " << y << endl;
  21.     cout << "x (0x" << &x << ") = " << x << endl;
  22.  
  23.     int *pi = &x;
  24.  
  25.     cout << "pi (0x" << &pi << ") = 0x" << pi << " = " << *pi << endl;
  26.  
  27.     int &r = *pi;
  28.     cout << "r (0x" << &r << ") = " << r << endl;
  29.  
  30.     r = 11;
  31.     cout << "y (0x" << &y << ") = " << y << endl;
  32.  
  33.     float v[ ] = { 3.4, 1.2, 4.9 };
  34.     cout << "v[ 1 ] = " << index( v, 1 ) << endl;
  35.     index( v, 1 ) = 7.2;
  36.     cout << "v[ 1 ] = " << index( v, 1 ) << endl;
  37.  
  38.     const float *pf = new float( 3.14159 );
  39.     cout << "pf (0x" << pf << ") = " << *pf  << endl;
  40.     delete pf;  // shouldn't be possible!
  41.  
  42.     float &(*pindex)(float *, int ) = &index;
  43.     pf = &pindex( v , 0 );
  44.     cout << "pf (0x" << pf << ") = " << *pf  << endl;
  45. }

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].