Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compound Types: References, Pointers Where are we going with this?

Similar presentations


Presentation on theme: "Compound Types: References, Pointers Where are we going with this?"— Presentation transcript:

1 Compound 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 What is a Compound 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 // good // error! int ival = 1024; int &refVal = ival; int &refVal2;

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 Symbol Table 0xfa10 0xfa58 0xfa54 0xfa50 0xfa4c 0xfa48 0xfa44 0xfa40 0xfa3c 0xfa38 0xfa34 0xfa30 0xfa2c 0xfa28 0xfa24 0xfa20 0xfa1c 0xfa18 0xfa14 AddressRAM 0x00000400 int ival = 1024; int &refVal = ival; int &refVal3 = refVal; Symbol Table is used by compiler to hold names types, and locations IdentifierTypeLocation ivalint0xfa20 refValint0xfa20 refVal3int0xfa20 As each variable is declared, the compiler adds the name (identifier) to the symbol table along with type and the memory slot (address/location/pointer) where the variable's value will be stored. References are just another name for the same slot.

7 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

8 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

9 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

10 0xfa10 0xfa58 0xfa54 0xfa50 0xfa4c 0xfa48 0xfa44 0xfa40 0xfa3c 0xfa38 0xfa34 0xfa30 0xfa2c 0xfa28 0xfa24 0xfa20 0xfa1c 0xfa18 0xfa14 AddressRAM 0x0000fa24 0x???????? 0x0000fa24 0x00000005 0x???????? 0x0000fa30 0x00000000 Symbol Table IdentifierTypeLocation ivalint0xfa20 refValint0xfa20 refVal3int0xfa20 int j=5, *ip1 = &j; int *ip2 = ip1; double f, *dp2 = &f; double g = 0.0; dp2 = &g; 0x00000400 jint0xfa24 ip1int*0xfa28 ip2int*0xfa2c fdbl0xfa30 dp2dbl*0xfa38 gdbl0xfa3c 0x0000fa3c

11 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

12 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

13 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!

14 Constants const especially useful as qualifier for parameters Lets reader know that value of parameter will not be changed in function Makes it impossible to change the value in the function! int f(const int x) {... }


Download ppt "Compound Types: References, Pointers Where are we going with this?"

Similar presentations


Ads by Google