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
Last revisionBoth sides next revision
cplusplus:pointers_2_slides [2019/03/31 21:04] mithatcplusplus:pointers_2_slides [2021/10/19 19:45] – [Pointer to constant data] mithat
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'' qualifier when used with regular variables:+
     * ''const'' variables cannot be changed.     * ''const'' variables cannot be changed.
     * ''const'' should be used on variables and function parameters when the variable or parameter's value should not change.     * ''const'' should be used on variables and function parameters when the variable or parameter's value should not change.
-  * ''const'' qualifier with pointers is more complicated:+ 
 +===== The const qualifier ===== 
 +  * ''const'' with pointers is more complicated:
     * What is constant? The pointer value or the value of the thing pointed to?     * What is constant? The pointer value or the value of the thing pointed to?
     * Answer is //either or both//.     * Answer is //either or both//.
Line 102: Line 104:
  
   * A **constant pointer** is a pointer whose value cannot change.   * A **constant pointer** is a pointer whose value cannot change.
-  * Constant pointers store a memory location that cannot be changed.+  * Constant pointers //store a memory location that cannot be changed//.
   * Must be initialized when declared.   * Must be initialized when declared.
-  * Use the ''const'' keyword between the ''*'' and the name of the pointer:+  * ''const'' keyword goes between the ''*'' and the name of the pointer:
  
 <code c++> <code c++>
 int *const myPtr = &x;  // constant pointer to a (non-constant) int int *const myPtr = &x;  // constant pointer to a (non-constant) int
 +                        // myPtr will always point to x
 </code> </code>
  
Line 137: Line 140:
   * A **pointer to constant data** cannot change the value of what it's pointing to.   * A **pointer to constant data** cannot change the value of what it's pointing to.
   * Can point to different things.   * Can point to different things.
-  * Use the ''const'' keyword to qualify the type being pointed to.+  * ''const'' keyword qualifies the type being pointed to.
  
 <code cpp> <code cpp>
cplusplus/pointers_2_slides.txt · Last modified: 2021/10/19 19:46 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki