Changes between Version 1 and Version 2 of SDAP/cpp3

Show
Ignore:
Timestamp:
06/18/08 12:55:45 (2 years ago)
Author:
sgtflame
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • SDAP/cpp3

    v1 v2  
    145145||yetAnotherValue||3|| 
    146146 
    147 == Constant Pointers == 
    148  
    149 == Exercise == 
     147== Exercise 1 == 
     148 
     149Edit your !FirstProgram application and make it look something like this. 
     150 
     151{{{ 
     152#!cpp 
     153int main(int _argc, char* _argv[]) 
     154{ 
     155    // Initialize three variables  
     156    int value1(0); 
     157    int value2(0); 
     158    int value3(0); 
     159 
     160    // Declare a pointer 
     161    int* pointer1; 
     162 
     163    pointer1 = &value1; 
     164    pointer1 = &value2; 
     165    pointer1 = &value3; 
     166 
     167    value1 = 1; 
     168    value2 = 2; 
     169    value3 = 3; 
     170 
     171    // Get the value of the pointer by dereferencing it. 
     172    int returnValue(*pointer1); 
     173 
     174    // Return it 
     175    return returnValue; 
     176} 
     177}}} 
     178 
     179 
     180What will the value of '''returnValue''' be when the program returns? 
     181 
     182Compile the program, then step through it with the debugger.  Make sure you set a breakpoint on the line '''return returnValue'''. 
     183 
     184When you get to that point, hover over the variable name '''returnValue''' so it will tell you the value.  Did you get it right? 
     185 
     186Also, notice when you hover over a pointer, there's a little '''+''' or ''plus'' symbol next to it.  If you click on that symbol, the little window will expand and you will be able to see the value that the pointer is addressing. 
     187 
     188If you didn't get the answer right and you don't understand, please ask questions in the forums or on IRC.  irc://208.109.123.232:16667/indiezen 
     189 
     190== Constant Pointers and Pointers to Constants == 
     191 
     192Ok, so we understand pointers now hopefully. 
     193 
     194Next, lets talk about '''constant pointers''' and '''pointers to constants''' 
     195 
     196In the previous C++ lesson we learned about '''constant variables''' using the keyword '''const'''. 
     197 
     198In this example, '''myValue''' is a constant integer that has (and always will have) a value of 10. 
     199 
     200{{{ 
     201#!cpp 
     202    const int myValue(10); 
     203}}} 
     204 
     205 
     206Extending this example, lets add a pointer to the variable. 
     207 
     208{{{ 
     209#!cpp 
     210    const int myValue(10); 
     211    const int* pPointerToMyValue(&myValue); 
     212}}} 
     213 
     214There, that was easy... '''pPointerToMyValue''' points to a constant integer and is initialized to the address of '''myValue'''. 
     215 
     216Now, note that pPointerToMyValue is not itself constant.  It only points to a constant value. 
     217 
     218In the following example, we declare and initialize another variable named '''anotherValue''' then we modify '''pPointerToMyValue''' so that it has the value of the address of '''anotherValue'''. 
     219 
     220{{{ 
     221#!cpp 
     222    const int myValue(10); 
     223    const int* pPointerToMyValue(&myValue); 
     224 
     225    const int anotherValue(14); 
     226    pPointerToMyValue = &anotherValue; 
     227}}} 
     228 
     229This is legal because '''pPointerToMyValue''' is not constant.  ''The value that is pointed to by '''pPointerToMyValue''' is constant, but the pointer itself is not constant.'' 
     230 
     231To make a pointer constant, you have to move the location of the keyword '''const'''. 
     232 
     233In this example we show '''myValue''' being initialized to 10.  Notice that '''myValue''' is not constant. 
     234 
     235In the following line, we declare '''pPointerToMyValue''' which is a constant pointer to an '''int'''.  This means that the pointer itself cannot change, however the value which it points to can change. 
     236 
     237In the third line of this example we show just that.  '''myValue''' is modified to have the value 14.  This is legal because '''myValue''' is not constant. 
     238 
     239{{{ 
     240#!cpp 
     241    int myValue(10); 
     242    int* const pPointerToMyValue(&myValue); 
     243 
     244    myValue = 14; 
     245}}} 
     246 
     247If we were to change the program to look like this: 
     248 
     249{{{ 
     250#!cpp 
     251    int myValue(10); 
     252    int* const pPointerToMyValue(&myValue); 
     253 
     254    myValue = 14; 
     255 
     256    int anotherValue(13); 
     257    pPointerToMyValue = &anotherValue;  /// WRONG!  pPointerToMyValue is const 
     258}}} 
     259 
     260... you would get a compile error. 
     261 
     262The following example would also cause a compile error. 
     263 
     264{{{ 
     265#!cpp 
     266    const int myValue(10); 
     267    const int* pPointerToMyValue(&myValue); 
     268 
     269    const int anotherValue(14); 
     270    pPointerToMyValue = &anotherValue;  /// Legal, pPointerToMyValue is not const 
     271 
     272    myValue = 12;  /// WRONG!  myValue is const 
     273}}} 
     274 
     275Now, we can also combine these two '''const''' modifiers. 
     276 
     277In the following example, '''pPointerToMyValue''' is a constant pointer that points to a constant value. 
     278 
     279{{{ 
     280#!cpp 
     281    const int myValue(10); 
     282    const int* const pPointerToMyValue(&myValue); 
     283 
     284    const int anotherValue(14); 
     285    pPointerToMyValue = &anotherValue;  /// WRONG!  pPointerToMyValue is const 
     286 
     287    myValue = 12;  /// WRONG!  myValue is const 
     288}}} 
     289 
     290== More Exercise == 
    150291 
    151292If you would like more text explaining pointers, feel free to read HTPCPP Chapter 8.2.  If you're an experienced programmer, please feel free to read all of Chapter 8 if you're interested. 
    152293 
    153 Edit your !FirstProgram and  
     294Edit your !FirstProgram and play around with '''int''' and '''int *''' data types and variables.  Be sure to use the debugger to see if the program behaves as you expected. 
     295 
     296Play around with the '''const''' and '''non-const''' variables and see if you can predict when you will get compiler errors due to assigning values to a constant variable. 
     297 
     298If you have any questions, please be sure to ask them in IRC or in the forums.