com pound types references pointers
play

Com pound Types: References, Pointers Where are we going with this? - PowerPoint PPT Presentation

Com pound Types: References, Pointers Where are we going with this? LHS and RHS LHS = Left Hand Side Means replace contents Set value where this thing is stored RHS = Right Hand Side Means value Evaluate expression,


  1. Com pound Types: References, Pointers Where are we going with this?

  2. LHS and RHS • LHS = Left Hand Side − Means replace contents − Set value where this thing is stored • RHS = Right Hand Side – Means value – Evaluate expression, function, etc. – Arrive at a value to store in LHS

  3. W hat is a Com pound Type? • A compound type is a type defined in terms of another type − There are several in C+ + − References and pointers included • Declaration = base type + list of declarators – Declarator = variable name and type related to base type

  4. References • A reference is just allows you to use another name for an object − When defined, we bind the name to the object referenced − The reference MUST be initialized − The reference CAN NOT be changed int ival = 1024; int &refVal = ival; // good int &refVal2; // error!

  5. References int ival = 1024; int &refVal = ival; // good int &refVal3 = refVal; // good refVal = 2; // now ival = 2 int j = refVal3; // now j = 2 also

  6. References • A reference is an alias for an object − Anything you do to it, you do to the object it refers to • Type of reference and object must match exactly int &refVa4 = 1; // error, not object double dVal = 1.0; int &refVal5 = dVal; // error, types

  7. Pointers • A pointer object is a compound type that holds the address (points) to another object − When defined, we do NOT have to initialize (though it is good practice) − A pointer may be reassigned − A pointer may have a pointer to it! • Pointer type must agree with object it points to − Otherwise, operations ambiguous

  8. Pointers int j=5, *ip1 = &j; // ip1 pts to j int *ip2 = ip1; // ip2 also pts to j double f, *dp2 = &f; // dp2 pts to f double g = 0.0; dp2 = &g; // dp2 now points to g dp2 = ip2; // error! Type mismatch ip2 = &f; // error! Type mismatch

  9. Symbol Table RAM Address 0xfa58 I dentifier Location 0xfa54 0xfa50 j 0xfa24 0xfa4c ip1 0xfa28 0xfa48 0xfa44 ip2 0xfa2c 0xfa40 0x00000000 0xfa3c 0x00000000 f 0xfa30 0x0000fa30 0xfa38 dp2 0xfa38 0x00000000 0xfa34 0x00000000 0xfa30 g 0xfa3c 0x0000fa24 0xfa2c 0x0000fa24 0xfa28 0x00000005 0xfa24 0xfa20 0xfa1c 0xfa18 0xfa14 0xfa10

  10. Pointers • A pointer may be in one of 4 states: 1)Pointing to an object 2)Pointing to location just past an object 3)Null pointer (not pointing at any object) 4)Invalid (none of the above) • Dereferencing uses * operator int cnt = 0, *ip6 = &cnt; *ip6 += 5; // now cnt = 5 cout << *ip6; // prints cnt value

  11. Pointers to Pointers • A pointer is an object • Unlike a reference, it may have a pointer to it • A pointer holds sufficient space for an address, NOT the object type it points to! • Defining a pointer DOES NOT allocate space for the object type at which it points! int cnt = 0; int *ip6 = &cnt; // ip6 pts at cnt int **ipp7 = &ip6; // ipp7 pts at ip6 int *ip8; // ip8 uninit’ed

  12. Constants • const qualifier makes a variable unchangable • Makes it easy to know what literals mean • Makes it impossible to change the value • Value must be given at initialization const int MAXNUM = 100; MAXNUM = 200; // illegal! const int NAME_LEN = get_size(); const int K; // error! Uninitialized!

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend