| 147 | | == Constant Pointers == |
| 148 | | |
| 149 | | == Exercise == |
| | 147 | == Exercise 1 == |
| | 148 | |
| | 149 | Edit your !FirstProgram application and make it look something like this. |
| | 150 | |
| | 151 | {{{ |
| | 152 | #!cpp |
| | 153 | int 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 | |
| | 180 | What will the value of '''returnValue''' be when the program returns? |
| | 181 | |
| | 182 | Compile the program, then step through it with the debugger. Make sure you set a breakpoint on the line '''return returnValue'''. |
| | 183 | |
| | 184 | When you get to that point, hover over the variable name '''returnValue''' so it will tell you the value. Did you get it right? |
| | 185 | |
| | 186 | Also, 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 | |
| | 188 | If 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 | |
| | 192 | Ok, so we understand pointers now hopefully. |
| | 193 | |
| | 194 | Next, lets talk about '''constant pointers''' and '''pointers to constants''' |
| | 195 | |
| | 196 | In the previous C++ lesson we learned about '''constant variables''' using the keyword '''const'''. |
| | 197 | |
| | 198 | In 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 | |
| | 206 | Extending 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 | |
| | 214 | There, that was easy... '''pPointerToMyValue''' points to a constant integer and is initialized to the address of '''myValue'''. |
| | 215 | |
| | 216 | Now, note that pPointerToMyValue is not itself constant. It only points to a constant value. |
| | 217 | |
| | 218 | In 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 | |
| | 229 | This 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 | |
| | 231 | To make a pointer constant, you have to move the location of the keyword '''const'''. |
| | 232 | |
| | 233 | In this example we show '''myValue''' being initialized to 10. Notice that '''myValue''' is not constant. |
| | 234 | |
| | 235 | In 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 | |
| | 237 | In 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 | |
| | 247 | If 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 | |
| | 262 | The 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 | |
| | 275 | Now, we can also combine these two '''const''' modifiers. |
| | 276 | |
| | 277 | In 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 == |