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? - - 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,
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
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
References
- A reference is just allows you to use
another name for an object
− When defined, we bind the name to the
- bject referenced
− The reference MUST be initialized − The reference CAN NOT be changed
// good // error! int ival = 1024; int &refVal = ival; int &refVal2;
References
int ival = 1024; int &refVal = ival; // good int &refVal3 = refVal; // good refVal = 2; // now ival = 2 int j = refVal3; // now j = 2 also
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
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
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
I dentifier Location j 0xfa24 ip1 0xfa28 ip2 0xfa2c f 0xfa30 dp2 0xfa38 g 0xfa3c Symbol Table
0xfa10 0xfa58 0xfa54 0xfa50 0xfa4c 0xfa48 0xfa44 0xfa40 0xfa3c 0xfa38 0xfa34 0xfa30 0xfa2c 0xfa28 0xfa24 0xfa20 0xfa1c 0xfa18 0xfa14 Address RAM 0x0000fa24 0x00000000 0x0000fa24 0x00000005 0x00000000 0x0000fa30 0x00000000 0x00000000
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
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
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