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 [2018/02/24 20:50] – [Pointers in the abstract] 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 133: Line 133:
 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 poking your fingers into arbitrary memory locations is a good way to corrupt your program's memory.+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:
Line 162: 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 177: 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>
  
 The ''*'' and ''&'' operators complement each other. In other words, the following expressions will all evaluate as true: The ''*'' and ''&'' operators complement each other. In other words, the following expressions will all evaluate as true:
cplusplus/pointers_1.1519505410.txt.gz · Last modified: 2018/02/24 20:50 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki