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 [2016/03/05 22:05] – [Pointers 1] mithatcplusplus:pointers_1 [2019/03/28 16:24] (current) – [Pointer operators] mithat
Line 11: 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 30: 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 50: 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 76: 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 86: 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 93: 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 124: 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>
  
 ==== nullptr/NULL pointers ==== ==== nullptr/NULL pointers ====
Line 157: 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 172: 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 190: 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 211: Line 216:
          << "*&aPtr = " << *&aPtr << endl;          << "*&aPtr = " << *&aPtr << endl;
     return 0;     return 0;
-}</file>+} 
 +</file>
  
cplusplus/pointers_1.1457215539.txt.gz · Last modified: 2016/03/05 22:05 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki