User Tools

Site Tools


cplusplus:pointers_1

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_1 [2014/07/19 19:14] – [Pointers in relation to computer hardware] mithatcplusplus:pointers_1 [2019/03/28 16:24] (current) – [Pointer operators] mithat
Line 2: Line 2:
 Pointer fundamentals, syntax, and operations.((Portions loosely adapted from: Pointer fundamentals, syntax, and operations.((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.))
 +
  
 ===== Introduction ===== ===== Introduction =====
Line 10: Line 11:
  
 ===== Pointer variables ===== ===== Pointer variables =====
-You can think of a **pointer variable** as a variable that stores the address of some other variable.+You can think of a **pointer variable** as a variable that stores the base address of some other variable.
  
 And? And?
Line 29: Line 30:
 If we were now to ask what the address of ''count'' is (which we will learn how to do shortly), the system would tell us ''count'' is located at its base address---58000 in this case.((FIXME This seems to be true for both little-endian and big-endian systems, but I need a decent reference on this to say this with certainty.)) If we were now to ask what the address of ''count'' is (which we will learn how to do shortly), the system would tell us ''count'' is located at its base address---58000 in this case.((FIXME This seems to be true for both little-endian and big-endian systems, but I need a decent reference on this to say this with certainty.))
  
-So, now let's say that (for whatever reason) we wanted to create a variable to store the address where ''count'' is located. That variable is what we would call a //pointer//, because it "points to" a block of memory. Extending the example above, let's say that we create a pointer variable named ''coutPtr'' and set it to point to the ''int'' that is ''count''. Furthermore, let's say that pointer variables on our system are stored in 8 byte (i.e., 64 bit) units, and that when we run the program the operating system places ''countPtr'' at an 8-byte block starting at 64002. That would yield something that looks like:+So, now let's say that (for whatever reason) we wanted to create a variable to store the address where ''count'' is located. That variable is what we would call a //pointer//, because it "points to" a block of memory. Extending the example above, let's say that we create a pointer variable named ''countPtr'' and set it to point to the ''int'' that is ''count''. Furthermore, let's say that pointer variables on our system are stored in 8 byte (i.e., 64 bit) units, and that when we run the program the operating system places ''countPtr'' at an 8-byte block starting at 64002. That would yield something that looks like:
  
 ^ Variable name ^ Memory location ^ Memory contents across all 8 bytes (64 bits) ^ ^ Variable name ^ Memory location ^ Memory contents across all 8 bytes (64 bits) ^
Line 49: Line 50:
 {{ :cplusplus:ptr02.png?nolink |}} {{ :cplusplus:ptr02.png?nolink |}}
  
-In this diagram, the //value// of the pointer ''countPtr'' is the arrow that points to the integer  ''count''.+In this diagram, the //value// of the pointer ''countPtr'' is the box the arrow points to (i.e., the integer  ''count'').
  
 Just as the value of a normal variable can change, the value of a pointer can change as well. Let’s say in addition to the variable ''count'' we also have a variable ''num'' that holds the value 2. If we change the value of ''countPtr'' to point to ''num'', the diagram would now look like: Just as the value of a normal variable can change, the value of a pointer can change as well. Let’s say in addition to the variable ''count'' we also have a variable ''num'' that holds the value 2. If we change the value of ''countPtr'' to point to ''num'', the diagram would now look like:
Line 75: Line 76:
 <code cpp>int* myPtr, anotherOne;   // a pointer and an int</code> <code cpp>int* myPtr, anotherOne;   // a pointer and an int</code>
  
-Pointers can be created to point to any type, but they //must// point to some type:+Pointers can be created to point to any type:
 <code cpp> <code cpp>
 char *yourPtr;  // declare a pointer to a char char *yourPtr;  // declare a pointer to a char
-double *herPtr; // declare a pointer to a double +double *zerPtr; // declare a pointer to a double 
-string *hisPtr; // declare a pointer to a string</code>+string *hyPtr // declare a pointer to a string 
 +</code>
  
 === Assignment === === Assignment ===
Line 85: Line 87:
 <code cpp>int num = -42; <code cpp>int num = -42;
 cout << num << endl;   // prints value held in variable num cout << num << endl;   // prints value held in variable num
-cout << &num << endl ; // prints the base address of variable num</code>+cout << &num << endl ; // prints the base address of variable num 
 +</code>
 Most environments show base addresses as [[http://www.cplusplus.com/doc/hex/|hexadecimal]] numbers by default. Most environments show base addresses as [[http://www.cplusplus.com/doc/hex/|hexadecimal]] numbers by default.
  
Line 92: Line 95:
 int y = 5;   // declare an integer variable y int y = 5;   // declare an integer variable y
 int *myPtr;  // declare a pointer to int int *myPtr;  // declare a pointer to int
-myPtr = &y;  // myPtr gets address of ("points to") y</code>+myPtr = &y;  // myPtr gets address of ("points to") y 
 +</code>
  
 The result of this code fragment may be diagrammed as follows: The result of this code fragment may be diagrammed as follows:
Line 123: Line 127:
  
 myPtr = &z;   // myPtr gets address of z myPtr = &z;   // myPtr gets address of z
-myPtr = &x;   // myPtr now has address of x</code>+myPtr = &x;   // myPtr now has address of x 
 +</code>
  
 === Initialization === === Initialization ===
 Pointer variables in C++ are not automatically initialized. This means that a pointer variable declaration along the lines of Pointer variables in C++ are not automatically initialized. This means that a pointer variable declaration along the lines of
 <code cpp>double *myPtr;</code> <code cpp>double *myPtr;</code>
-leaves the pointer pointing to an arbitrary memory location. This is dangerous because you should not go poking your fingers into arbitrary memory locations.+leaves the pointer pointing to an arbitrary memory location. This is dangerous because poking your fingers into arbitrary memory locations is a good way to corrupt your program's memory or create other headaches.
  
 Pointer variables can be initialized when declared. It is good programming practice to always initialize pointers so they do not accidentally point to unknown memory locations. The code below initializes the value of the pointer variable in the declaration: Pointer variables can be initialized when declared. It is good programming practice to always initialize pointers so they do not accidentally point to unknown memory locations. The code below initializes the value of the pointer variable in the declaration:
 <code cpp> <code cpp>
 int y = 5; int y = 5;
-int *myPtr = &y;  // myPtr gets address of y</code>+int *myPtr = &y;  // myPtr gets address of y 
 +</code>
  
-==== NULL pointers ====+==== nullptr/NULL pointers ====
  
-You can set a pointer to a special value that indicates that it is //pointing to nothing//. This special value has the name ''NULL''.+You can set a pointer to a special value that indicates that it is //pointing to nothing//. This special value has the name ''nullptr'' (C++11) or ''NULL''.
  
-If the memory location that a pointer variable will point to isn’t known at the time it is declared, then you should initialize it to ''NULL''. This will prevent the pointer from accidentally pointing to arbitrary memory locations.+If the memory location that a pointer variable will point to isn’t known at the time it is declared, then you should initialize it to ''nullptr'' or ''NULL''. This will prevent the pointer from accidentally pointing to arbitrary memory locations.
  
 Below is an example of initializing a pointer variable to ''NULL'' when declaring it. Below is an example of initializing a pointer variable to ''NULL'' when declaring it.
 <code cpp> <code cpp>
-int *yourPtr = NULL   // yourPtr points to nothing+int *yourPtr = nullptr; // yourPtr points to nothing
 int y = 5; int y = 5;
 yourPtr = &y;           // yourPtr gets address of y</code> yourPtr = &y;           // yourPtr gets address of y</code>
  
-Note how ''yourPtr'' is initialized to ''NULL'' even though we change the pointer immediately afterwards. We did this because it's a good habit to initialize your pointers so they //never// point to unknown memory locations.+Note how ''yourPtr'' is initialized to ''nullptr'' even though we change the pointer immediately afterwards. We did this because it's a good habit to initialize your pointers so they //never// point to unknown memory locations.
  
-''NULL'' has an integer value of 0, so sometimes you will see pointer variables set to 0 instead of ''NULL''. Similarly, the character '''\0''' also has an integer value of 0, so sometimes you will see pointers set to '''\0''' instead of ''NULL''. All three have the same effect: they make the pointer point to nothing.+''nullptr'' and ''NULL'' have an integer value of 0, so sometimes you will see pointer variables set to 0 instead. Similarly, the character '''\0''' also has an integer value of 0, so sometimes you will see pointers set to '''\0'''. All three have the same effect: they make the pointer point to nothing.
  
 ==== Pointer operators ==== ==== Pointer operators ====
Line 156: Line 162:
  
 === Indirection/dereferencing operator === === Indirection/dereferencing operator ===
-The **indirection** or **dereferencing** operator, ''*'', returns the value of what its operand points to. For example, if ''myPtr'' points to variable ''y'', ''*myPtr'' returns the value stored in ''y''.+The **indirection** or **dereferencing** operator, ''*'', accesses the value of what its operand points to. For example, if ''myPtr'' points to variable ''y'', ''*myPtr'' returns the value stored in ''y''.
 <code cpp> <code cpp>
 int y = -1;       // declare y and initialize its value int y = -1;       // declare y and initialize its value
Line 171: Line 177:
 cout << y;        // prints 7</code> cout << y;        // prints 7</code>
  
-Think of the indirection/dereferencing operator as meaning, “the_thing_at_the_end_of_”, as in:+You can think of the indirection/dereferencing operator as meaning, “the_thing_at_the_end_of_”, as in:
  
 <code>the_thing_at_the_end_of_myPtr = 7;</code> <code>the_thing_at_the_end_of_myPtr = 7;</code>
Line 189: Line 195:
 int main() int main()
 { {
-    int a;             // a is an integer +    int a;                  // a is an integer 
-    int *aPtr = NULL // aPtr is a pointer to an integer +    int *aPtr = nullptr   // aPtr is a pointer to an integer 
- +  
-    a = 7;             // give a a value +    a = 7;      // give a a value 
-    aPtr = &a;         // set aPtr to the address of a +    aPtr = &a;  // set aPtr to the address of a 
 + 
     cout << "The value of a is:     " << a << endl     cout << "The value of a is:     " << a << endl
          << "The address of a is:   " << &a << endl          << "The address of a is:   " << &a << endl
          << "The value of aPtr is:  " << aPtr << endl;          << "The value of aPtr is:  " << aPtr << endl;
 + 
     cout << endl;     cout << endl;
 + 
     cout << "The value of a is:     " << a << endl     cout << "The value of a is:     " << a << endl
          << "The value of *aPtr is: " << *aPtr << endl;          << "The value of *aPtr is: " << *aPtr << endl;
     cout << endl;     cout << endl;
 + 
     cout << "Showing that * and & are inverses of each other:"     cout << "Showing that * and & are inverses of each other:"
          << endl          << endl
Line 210: Line 216:
          << "*&aPtr = " << *&aPtr << endl;          << "*&aPtr = " << *&aPtr << endl;
     return 0;     return 0;
-}</file>+} 
 +</file>
  
cplusplus/pointers_1.1405797292.txt.gz · Last modified: 2014/07/19 19:14 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki