User Tools

Site Tools


cplusplus:pointers_1_slides

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_slides [2019/03/28 16:19] mithatcplusplus:pointers_1_slides [2021/10/19 19:29] (current) – [Example] mithat
Line 2: Line 2:
 ~~NOTOC~~ ~~NOTOC~~
  
-====== Pointers 1 ======+====== Pointers 1 slides ======
 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.))\\ 
 Mithat Konar\\ Mithat Konar\\
-March 282019+October 192021
  
 ===== Introduction ===== ===== Introduction =====
Line 23: Line 23:
  
 ===== Pointer hardware model ===== ===== Pointer hardware model =====
-Assume an ''int'' named ''count'' storing the value 7 and ''int''s occupy 4 bytes.+  * Assume an ''int'' named ''count'' storing the value 7 and ''int''s occupy 4 bytes.
  
 ^ Variable name ^ Memory location ^ Memory contents across all four bytes (32 bits)^ ^ Variable name ^ Memory location ^ Memory contents across all four bytes (32 bits)^
Line 31: Line 31:
 |:::|58003|:::| |:::|58003|:::|
  
-The address of ''count'' is the base address 58000.  +  * The address of ''count'' is the base address 58000. 
  
 ===== Pointer hardware model ===== ===== Pointer hardware model =====
-A //pointer// variable set to point to ''count'' would store the value 58000.+ 
 +  * A //pointer// variable set to point to ''count'' would store the value 58000.
  
 ^ Variable name ^ Memory location ^ Memory contents across all 8 bytes (64 bits) ^ ^ Variable name ^ Memory location ^ Memory contents across all 8 bytes (64 bits) ^
-|''countPtr''|64002|''00000000000000000000000000000000\\ 00000000000000001110001010010000''\\ (i.e., 58000 in decimal)|+|''countPtr''|64002|''00000000000000000000000000000000\\ 00000000000000001110001010010000''\\ (58000 in decimal)|
 |:::|64003|:::| |:::|64003|:::|
 |:::|...|:::| |:::|...|:::|
 |:::|64009|:::| |:::|64009|:::|
  
-The //value// of the pointer variable is the base address it stores.+  * The //value// of pointer variable is the base address of another variable.
  
 ===== Pointer visual model ===== ===== Pointer visual model =====
Line 60: Line 61:
 ===== Pointer syntax basics ===== ===== Pointer syntax basics =====
  
-===== Declaration =====+===== Pointer declaration =====
   * ''*'' character is used to indicate pointer variables in declarations and function parameter lists.   * ''*'' character is used to indicate pointer variables in declarations and function parameter lists.
   * Type of data being pointed to must also be indicated.   * Type of data being pointed to must also be indicated.
Line 77: Line 78:
 </code> </code>
  
-===== Declaration =====+===== Pointer declaration =====
  
   * You can declare more than one pointer variable at a time:   * You can declare more than one pointer variable at a time:
Line 99: Line 100:
   * Most environments show base addresses as [[http://www.cplusplus.com/doc/hex/|hexadecimal]] numbers.   * Most environments show base addresses as [[http://www.cplusplus.com/doc/hex/|hexadecimal]] numbers.
  
-===== Assignment =====+===== Pointer assignment =====
  
   *  Declare an integer variable ''y'' and a pointer variable ''myPtr'' set to store the address of ("point to") ''y''.   *  Declare an integer variable ''y'' and a pointer variable ''myPtr'' set to store the address of ("point to") ''y''.
Line 110: Line 111:
 {{:cplusplus:ptr04a.png?nolink|}} {{:cplusplus:ptr04a.png?nolink|}}
  
-===== Assignment =====+===== Pointer assignment =====
  
   *  Declare an integer variable ''y'' and a pointer variable ''myPtr'' set to store the address of ("point to") ''y''.   *  Declare an integer variable ''y'' and a pointer variable ''myPtr'' set to store the address of ("point to") ''y''.
Line 129: Line 130:
  
  
-===== Assignment =====+===== Pointer assignment =====
  
-  * Chaning the value of pointer variables:+  * Changing the value of pointer variables:
  
 <code cpp> <code cpp>
Line 142: Line 143:
 </code> </code>
  
-===== Initialization =====+===== Pinter initialization =====
   * Local pointer variables in C++ are not automatically initialized.    * Local pointer variables in C++ are not automatically initialized. 
   * Uninitialized pointers point to arbitrary memory. //Very dangerous.//   * Uninitialized pointers point to arbitrary memory. //Very dangerous.//
Line 164: Line 165:
   * ''nullptr'' and ''NULL'' have an integer value of ''0'', so sometimes programmers use ''0'' or '''\0'''.   * ''nullptr'' and ''NULL'' have an integer value of ''0'', so sometimes programmers use ''0'' or '''\0'''.
  
-===== Pointer operators =====+===== Operators for pointers =====
  
-  *  **address operator** ''&'' (above). +  *  **address operator** ''&'' (already covered). 
-  *  **indirection** or **dereferencing operator** ''*'' (below).+  *  **indirection** or **dereferencing operator** ''*'' (covered next).
  
 ===== Indirection/dereferencing operator ===== ===== Indirection/dereferencing operator =====
  
-  * **indirection** or **dereferencing** operator, ''*'', returns the value of what its operand points to.+  * **indirection** or **dereferencing** operator, ''*'', accesses the value of what its operand points to.
  
 <code cpp> <code cpp>
Line 189: Line 190:
 *myPtr = 7;       // change value in y to 7 *myPtr = 7;       // change value in y to 7
 cout << y;        // prints 7</code> cout << y;        // prints 7</code>
- 
-===== Indirection/dereferencing operator ===== 
  
   * You can 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:
Line 222: Line 221:
          << "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;
    
Line 233: Line 231:
          << "&*aPtr = " << &*aPtr << endl          << "&*aPtr = " << &*aPtr << endl
          << "*&aPtr = " << *&aPtr << endl;          << "*&aPtr = " << *&aPtr << endl;
 +
     return 0;     return 0;
 } }
 </file> </file>
  
cplusplus/pointers_1_slides.1553789958.txt.gz · Last modified: 2019/03/28 16:19 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki