User Tools

Site Tools


cplusplus:pointers_2

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 [2016/03/05 22:21] – [Pointers as function parameters] mithatcplusplus:pointers_2 [2019/03/28 16:55] (current) mithat
Line 3: Line 3:
 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.))
- 
-<WRAP center round important 60%> 
-This content has not been vetted for C++11 compliance. 
-</WRAP> 
- 
 ===== Comparing pointers ===== ===== Comparing pointers =====
  
Line 28: Line 23:
  
 <file cpp function-with-pointer.cpp> <file cpp function-with-pointer.cpp>
-/** Demostrates how to pass pointers to functions. */+/** Demonstrates how to pass pointers to functions. */
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
Line 45: Line 40:
 } }
  
-/** Simple function showing how to pass pointers to functions. +/*
-  * Print the value of nPtr and the value of dereferenced nPtr. + * Simple function showing how to pass pointers to functions. 
-  */+ * Print the value of nPtr and the value of dereferenced nPtr. 
 + */
 void printPtr(int *nPtr) void printPtr(int *nPtr)
 { {
Line 58: Line 54:
 ===== Calling functions by reference ===== ===== Calling functions by reference =====
  
-Pointers can be used to implement //call-by-reference// in functions. The ''*'' operator is used to alias a variable inside of function.  +Pointers can be used to implement //passing by reference// in functions. The ''*'' operator is used to alias a variable inside of function.  
  
 <code cpp> <code cpp>
Line 67: Line 63:
 </code> </code>
  
-In the function above, ''*number'' is used as an alias for the variable passed in. Strictly speaking, the passing mechanism is //call-by-value// because the argument that is passed into the function (an address) is copied to the formal pointer parameter ''number''. However, dereferencing ''number'' acts as an alias to whatever is passed in.+In the function above, ''*number'' is used as an alias for the variable passed in. Strictly speaking, the passing mechanism is //pass by value// because the argument that is passed into the function (an address) is copied to the formal pointer parameter ''number''. However, dereferencing ''number'' acts as an alias to whatever is passed in.
  
  
 <file cpp cube-by-reference.cpp> <file cpp cube-by-reference.cpp>
-/** Cube a variable using call-by-reference with a pointer. */+/** Cube a variable using pass by reference with a pointer. */
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
Line 88: Line 84:
 void cubeByReference(int *nPtr) void cubeByReference(int *nPtr)
 { {
-    *nPtr = *nPtr * *nPtr * *nPtr;+    *nPtr = (*nPtr(*nPtr(*nPtr)// parenthesis for readability
 } }
 </file> </file>
 +
 +<WRAP center tip 90%>
 +I feel pointers are much better than reference parameters for implementing pass by  reference because the syntax in the function invocation (e.g., ''cubeByReference(&number))'' makes it clear that a pointer is involved --- which is good indication that pass by  reference is being used.
 +</WRAP>
  
 ===== The const qualifier and pointers ===== ===== The const qualifier and pointers =====
  
-You are already familiar with C++'s ''const'' qualifier when used with regular variables. In brief:+You are already familiar with C++'s ''const'' qualifier when used with regular variables. To review:
  
   * ''const'' variables cannot be changed (i.e., they become //constants//).   * ''const'' variables cannot be changed (i.e., they become //constants//).
Line 135: Line 135:
 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**. 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**.
  
-To create a pointer to constant, use the ''const'' keyword to qualify the type being pointed to.+To create a pointer to constant data, use the ''const'' keyword to qualify the type being pointed to.
  
 <code cpp> <code cpp>
Line 144: Line 144:
 myPtr = &y;    // no problem, myPtr now points to y myPtr = &y;    // no problem, myPtr now points to y
 </code> </code>
 +
 +
 +<WRAP center tip 90%>
 +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 ==== ==== Constant pointer to constant data ====
Line 156: Line 169:
 myPtr = &y;    // syntax error! myPtr = &y;    // syntax error!
 </code> </code>
- 
- 
- 
  
cplusplus/pointers_2.1457216483.txt.gz · Last modified: 2016/03/05 22:21 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki