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 [2014/04/22 04:24] – [The const qualifier and pointers] 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.))
- 
 ===== Comparing pointers ===== ===== Comparing pointers =====
  
Line 14: Line 13:
 double *bPtr = &y; double *bPtr = &y;
  
-if (aPtr == bPtr)   // compare addresses +if (aPtr == bPtr) ...   // compare addresses 
-if (*aPtr == *bPtr) // compare contents of addresses, i.e. same as +if (*aPtr == *bPtr) ... // compare contents of addresses, i.e. same as 
-if (x == y)+if (x == y) ...
 </code> </code>
  
Line 24: 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 41: Line 40:
 } }
  
-/** Simple function showing how to pass pointers to functions. */+/*
 + * 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 52: 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 61: 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 82: 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 100: Line 106:
 ==== Constant pointers ==== ==== Constant pointers ====
  
-A **constant pointer** is a 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 declared and initialized, //a constant pointer cannot be reassigned//.+A **constant pointer** is a 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: To declare a constant pointer, use the ''const'' keyword between the ''*'' and the name of the pointer:
  
-<code cpp> +<file cpp const-ptr-demo.cpp> 
-int x = 2, y = 5; +/** Attempting to modify a constant pointer to non-constant data. */ 
-int *const myPtr = &x;  // constant pointer to a non-constant int+#include <iostream> 
 +using namespace std; 
 +int main() 
 +
 +    int x = 2, y = 5; 
 +    int * const myPtr = &x;  // constant pointer to a (non-constantint 
 +                             // The data pointed to by myPtr can be  
 +                             // modified through myPtr, but myPtr must 
 +                             // always point to the same memory location.
  
-*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!
-</code>+ 
 +    cout << *myPtr << endl; 
 + 
 +    return 0; 
 +} 
 +</file>
  
-==== Pointer to constant ====+==== 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** or **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**.
  
-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 126: Line 145:
 </code> </code>
  
-==== Constant pointer to constant ====+ 
 +<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 ====
  
 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." 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."
Line 137: Line 169:
 myPtr = &y;    // syntax error! myPtr = &y;    // syntax error!
 </code> </code>
- 
-==== Example ==== 
- 
-<file cpp const-ptr-demo.cpp> 
-/** Attempting to modify a constant pointer to non-constant data. */ 
-#include <iostream> 
-using namespace std; 
-int main() 
-{ 
-    int x, y; 
-    int * const ptr = &x;   // ptr is a constant pointer to an 
-                            // integer. The integer can be modified 
-                            // through ptr, but ptr always points 
-                            // to the same memory location. 
- 
-    *ptr = 7; 
-    ptr = &y;   // error 
- 
-    return 0; 
-} 
-</file> 
- 
  
cplusplus/pointers_2.1398140650.txt.gz · Last modified: 2014/04/22 04:24 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki