User Tools

Site Tools


cplusplus:pointers_2_slides

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
cplusplus:pointers_2_slides [2019/03/28 17:00] mithatcplusplus:pointers_2_slides [2021/10/19 19:46] (current) – [Constant pointer to constant data] mithat
Line 2: Line 2:
 ~~NOTOC~~ ~~NOTOC~~
  
-====== Pointers 2 ======+====== Pointers 2 slides ======
 Pointers and comparisons, functions, and qualifiers.((Portions loosely adapted from: Pointers and comparisons, functions, and qualifiers.((Portions loosely adapted from:
 Deitel, Harvey M., and Paul J. Deitel. "Pointers and Strings." In //C++: How to Program//. 3 ed. Upper Saddle River, NJ: Prentice Hall, 2001. 304-388.))\\  Deitel, Harvey M., and Paul J. Deitel. "Pointers and Strings." In //C++: How to Program//. 3 ed. Upper Saddle River, NJ: Prentice Hall, 2001. 304-388.))\\ 
Line 28: Line 28:
   * Arguments can be anything that can be assigned to a pointer (i.e., addresses or pointers).   * Arguments can be anything that can be assigned to a pointer (i.e., addresses or pointers).
  
 +===== Example =====
 <file cpp function-with-pointer.cpp> <file cpp function-with-pointer.cpp>
 /** Demonstrates how to pass pointers to functions. */ /** Demonstrates how to pass pointers to functions. */
Line 62: Line 63:
   * Pointers can be used to implement //pass by reference// in functions.   * Pointers can be used to implement //pass by reference// in functions.
   * The ''*'' operator is used to alias a variable inside of function.     * The ''*'' operator is used to alias a variable inside of function.  
-  * In the function below, ''*number'' is used as an alias for the variable passed in. +  * Strictly speaking, the passing mechanism is //call by value//. But functionality is pass by reference. 
-  * Strictly speaking, the passing mechanism is //call by value//. But functionally it'pass by reference. +  * Syntax in function invocation (e.g., ''cubeByReference(&number))'' makes clear that a pointer is involved --- good indication that call by reference is used in the function.
-  * The syntax in the function invocation (e.g., ''cubeByReference(&number))'' makes it clear that a pointer is involved --- which is good indication that call by reference is being used.+
  
 +===== Example =====
 <file cpp cube-by-reference.cpp> <file cpp cube-by-reference.cpp>
 /** Cube a variable using pass by reference with a pointer. */ /** Cube a variable using pass by reference with a pointer. */
Line 84: Line 85:
 void cubeByReference(int *nPtr) void cubeByReference(int *nPtr)
 { {
 +    // *nptr'' is an alias for the variable passed in.
     *nPtr = (*nPtr) * (*nPtr) * (*nPtr); // parenthesis for readability     *nPtr = (*nPtr) * (*nPtr) * (*nPtr); // parenthesis for readability
 } }
 </file> </file>
  
----------------------------------------------------------------------------------------------------------------+===== The const qualifier =====
  
-===== The const qualifier and pointers =====+  * ''const'' when used with regular variables: 
 +    * ''const'' variables cannot be changed. 
 +    * ''const'' should be used on variables and function parameters when the variable or parameter's value should not change.
  
-You are already familiar with C++'''const'' qualifier when used with regular variablesTo review:+===== The const qualifier ===== 
 +  * ''const'' with pointers is more complicated: 
 +    * What is constant? The pointer value or the value of the thing pointed to? 
 +    * Answer is //either or both//.
  
-  * ''const'' variables cannot be changed (i.e., they become //constants//). +===== Constant pointers =====
-  * ''const'' should be used on a function parameter when the function does not need to change that parameter's value. +
-  * Attempting to change a ''const'' variable or parameter is a syntax error (which should be caught by the compiler).+
  
-Applying the ''const'' qualifier to pointers is a little more complicated. What is constant? The pointer value or the value of the thing pointed to?+  * A **constant pointer** is a pointer whose value cannot change. 
 +  * Constant pointers //store a memory location that cannot be changed//
 +  * Must be initialized when declared. 
 +  * ''const'' keyword goes between the ''*'' and the name of the pointer:
  
-The answer is //either or both//. +<code c++> 
- +int *const myPtr = &x;  // constant pointer to (non-constant) int 
-==== Constant pointers ==== +                        // myPtr will always point to 
- +</code>
-A **constant pointer** is pointer whose value cannot change; that is, constant pointers store a memory location that cannot be changed. Constant pointers must be initialized when declared, and once initialized cannot be reassigned. In other words, a constant pointer always points to the same chunk of memory.+
  
-To declare a constant pointer, use the ''const'' keyword between the ''*'' and the name of the pointer:+===== Example =====
  
 <file cpp const-ptr-demo.cpp> <file cpp const-ptr-demo.cpp>
Line 115: Line 122:
 { {
     int x = 2, y = 5;     int x = 2, y = 5;
-    int * const myPtr = &x;  // constant pointer to a (non-constant) int+    int *const myPtr = &x;  // constant pointer to a (non-constant) int
                              // The data pointed to by myPtr can be                               // The data pointed to by myPtr can be 
                              // modified through myPtr, but myPtr must                              // modified through myPtr, but myPtr must
Line 121: Line 128:
  
     *myPtr = 99;   // change value stored in x to 99     *myPtr = 99;   // change value stored in x to 99
-    myPtr = &y;    // syntax error!!+    myPtr = &y;    // syntax error!! can't point to a different int
  
     cout << *myPtr << endl;     cout << *myPtr << endl;
Line 129: Line 136:
 </file> </file>
  
-==== Pointer to constant data ====+===== Pointer to constant data =====
  
-You can also create a pointer that is itself not constant (i.e., it can be reassigned or can change "what is being pointed to") but that cannot change the value of "what is being pointed to." This is a **pointer to constant data** or simply **pointer to constant**+  * A **pointer to constant data** cannot change the value of what it's pointing to. 
- +  Can point to different things
-To create a pointer to constant data, use the ''const'' keyword to qualify the type being pointed to.+  ''const'' keyword qualifies the type being pointed to.
  
 <code cpp> <code cpp>
Line 139: Line 146:
 const int *myPtr = &x;  // non-constant pointer to a constant int const int *myPtr = &x;  // non-constant pointer to a constant int
  
-*myPtr = 99;   // syntax error!+*myPtr = 99;   // syntax error! can't modify what it's pointing to
 myPtr = &y;    // no problem, myPtr now points to y myPtr = &y;    // no problem, myPtr now points to y
 </code> </code>
  
 +===== Constant pointer to constant data =====
  
-<WRAP center tip 90%> +  * **Constant pointer to constant data** cannot change either "what it is pointing to" or the value of "what it is pointing to."
-One way to remember this is //"''const'' modifies the thing immediately following it."// +
- +
-In the case of: +
-<code c++>int *const myPtr = &x;</code> +
-''const'' modifies ''myPtr'', meaning the value of ''myPtr'' is constant. +
- +
-In the case of: +
-<code c++>const int *myPtr = &x;</code> +
-''const'' modifies ''int'', meaning the value of the ''int'' is constant. +
-</WRAP> +
- +
-==== Constant pointer to constant data ==== +
- +
-Combining the above, you can create a pointer that can change neither "what it is pointing to" nor the value of "what it is pointing to."+
  
 <code cpp> <code cpp>
Line 166: Line 160:
 *myPtr = 99;   // syntax error! *myPtr = 99;   // syntax error!
 myPtr = &y;    // syntax error! myPtr = &y;    // syntax error!
 +</code>
 +
 +===== How to remember =====
 +
 +  * ''const'' modifies the thing immediately following it.
 +
 +<code c++>
 +/* const modifies myPtr, meaning the value of myPtr is constant. */
 +int *const myPtr = &x;
 +</code>
 +
 +
 +<code c++>
 +/* const modifies int, meaning the value of the int is constant. */
 +const int *myPtr = &x;
 </code> </code>
  
cplusplus/pointers_2_slides.1553792410.txt.gz · Last modified: 2019/03/28 17:00 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki