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 15:29] 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\\ 
 +October 19, 2021
  
 ===== Introduction ===== ===== Introduction =====
Line 11: Line 12:
   * Important in C++ because a close relationship between pointers and arrays, C strings, and references.   * Important in C++ because a close relationship between pointers and arrays, C strings, and references.
  
-===== Address of a variable =====+===== Pointer variables =====
   * Variables are stored in blocks of computer memory.   * Variables are stored in blocks of computer memory.
-  * The **base address** of a variable is the memory address corresponding to the starting point of this block. +  * **base address**the memory address of the first byte of this block.
- +
-===== Pointer variables =====+
   * **pointer variable**: a variable that stores the base address of some other variable.   * **pointer variable**: a variable that stores the base address of some other variable.
   * Usefulness will be seen after understanding how to use them.   * Usefulness will be seen after understanding how to use them.
Line 24: Line 23:
  
 ===== Pointer hardware model ===== ===== Pointer hardware model =====
-Assume an ''int'' named ''count'' storing the value 7 and an ''int'' occupies 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)^
-|''count''|58000|''00000000000000000000000000000111'' (i.e. 7 in decimal)|+|''count''|58000|''00000000000000000000000000000111''\\ (i.e. 7 in decimal)|
 |:::|58001|:::| |:::|58001|:::|
 |:::|58002|:::| |:::|58002|:::|
 |:::|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 52: Line 52:
 {{ :cplusplus:ptr02.png?nolink |}} {{ :cplusplus:ptr02.png?nolink |}}
  
-  * The //value// of ''countPtr'' is the box the arrow points to (i.e., the integer  ''count'').+The //value// of ''countPtr'' is the box the arrow points to (i.e., the integer  ''count'').
  
 ===== Pointer visual model ===== ===== Pointer visual model =====
  
-If we change the value of ''countPtr'' to point to ''num'', the diagram would now look like:+Changing the value of ''countPtr'' to point to ''num'':
 {{ :cplusplus:ptr03.png?nolink |}} {{ :cplusplus:ptr03.png?nolink |}}
  
 ===== Pointer syntax basics ===== ===== Pointer syntax basics =====
  
-===== Declaration ===== +===== Pointer declaration ===== 
-In C++, the ''*'' character is used to indicate pointer variables in declarations and function parameter lists. The code fragment below creates a pointer variable named ''myPtr'' that can point to a block of memory that stores an ''int'':+  ''*'' character is used to indicate pointer variables in declarations and function parameter lists. 
 +  * Type of data being pointed to must also be indicated.
  
-<code cpp>int *myPtr;    // declare a pointer to an int</code>+<code cpp> 
 +int *myPtr;     // declare a pointer to an int 
 +bool *yourPtr;  // declare a pointer to a bool 
 +</code> 
 + 
 +  * Location of the ''*'' character is flexible:
  
-The location of the ''*'' character is flexible: it can be placed before the name of the pointer (as in the above example), immediately after the type, or separated from both the type and the name by whitespace. All the declarations below are equivalent: 
 <code cpp> <code cpp>
 int *myPtr; int *myPtr;
Line 72: Line 77:
 int * myPtr; int * myPtr;
 </code> </code>
-You can declare more than one pointer variable at a time:+ 
 +===== Pointer declaration ===== 
 + 
 +  * You can declare more than one pointer variable at a time:
  
 <code cpp>int *myPtr, *anotherOne;   // declare two pointers to an int</code> <code cpp>int *myPtr, *anotherOne;   // declare two pointers to an int</code>
  
-But be careful: the following actually creates one pointer and one ''int'':+  * But be careful::
  
 <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: +===== Address operator ===== 
-<code cpp> +  * To set the value of a pointer, you need the address of something. 
-char *yourPtr;  // declare a pointer to a char +  **address operator** ''&'' returns the base address of variable:
-double *zerPtr; // declare a pointer to a double +
-string *hyPtr;  // declare pointer to a string +
-</code>+
  
-=== Assignment === +<code cpp> 
-To set the value of a pointer, you need the address of something. C++ has an **address operator**, ''&'', that returns the base address of a variable: +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 cout << &num << endl ; // prints the base address of variable num
 </code> </code>
-Most environments show base addresses as [[http://www.cplusplus.com/doc/hex/|hexadecimal]] numbers by default. 
  
-The example below declares an integer variable ''y'' that is initialized to 5 and a pointer variable to an ''int'' named ''myPtr'' that is set to store the address of (i.e., "point to") ''y''.+  * Most environments show base addresses as [[http://www.cplusplus.com/doc/hex/|hexadecimal]] numbers. 
 + 
 +===== Pointer assignment ===== 
 + 
 +  *  Declare an integer variable ''y'' and a pointer variable ''myPtr'' set to store the address of ("point to") ''y''.
 <code cpp> <code cpp>
 int y = 5;   // declare an integer variable y int y = 5;   // declare an integer variable y
Line 101: Line 108:
 myPtr = &y;  // myPtr gets address of ("points to") y myPtr = &y;  // myPtr gets address of ("points to") y
 </code> </code>
- 
-The result of this code fragment may be diagrammed as follows: 
  
 {{:cplusplus:ptr04a.png?nolink|}} {{:cplusplus:ptr04a.png?nolink|}}
  
-and when run may generate the following memory map:+===== Pointer assignment ===== 
 + 
 +  *  Declare an integer variable ''y'' and a pointer variable ''myPtr'' set to store the address of ("point to") ''y''
 +<code cpp> 
 +int y = 5;   // declare an integer variable y 
 +int *myPtr;  // declare a pointer to int 
 +myPtr = &y;  // myPtr gets address of ("points to") y 
 +</code>
  
 ^ Variable name ^ Memory location ^ Memory contents  ^ ^ Variable name ^ Memory location ^ Memory contents  ^
 |''y''|52000|5| |''y''|52000|5|
-|:::|52001|:::+|:::|...|:::|
-|:::|52002|:::|+
 |:::|52003|:::| |:::|52003|:::|
 |...|...|...| |...|...|...|
 |''myPtr''|63002|52000| |''myPtr''|63002|52000|
-|:::|63003|:::+|:::|...|:::|
-|:::|63004|:::+
-|:::|63005|:::+
-|:::|63006|:::+
-|:::|63007|:::+
-|:::|63008|:::|+
 |:::|63009|:::| |:::|63009|:::|
  
-You can change the value of pointer variables (that's why it's called a //variable//!):+ 
 +===== Pointer assignment ===== 
 + 
 +  * Changing the value of pointer variables:
  
 <code cpp> <code cpp>
Line 134: Line 143:
 </code> </code>
  
-=== Initialization === +===== Pinter initialization ===== 
-Pointer variables in C++ are not automatically initialized. This means that a pointer variable declaration along the lines of +  * Local pointer variables in C++ are not automatically initialized.  
-<code cpp>double *myPtr;</code> +  Uninitialized pointers point to arbitrary memory. //Very dangerous.// 
-leaves the pointer pointing to an arbitrary memory locationThis is dangerous because poking your fingers into arbitrary memory locations is a good way to corrupt your program's memory or create other headaches.+  * You can initialize when declared:
  
-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;
Line 145: Line 153:
 </code> </code>
  
-==== nullptr/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 ''nullptr'' (C++11) or ''NULL''.+  * You can set a pointer to a value that indicates that it is //pointing to nothing//''nullptr'' (C++11) or ''NULL'' (earlier versions). 
 +  * Otherwise uninitialized pointers should be set to ''nullptr'' (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 ''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. 
 <code cpp> <code cpp>
 int *yourPtr = nullptr; // yourPtr points to nothing int *yourPtr = nullptr; // yourPtr points to nothing
Line 157: Line 163:
 yourPtr = &y;           // yourPtr gets address of y</code> yourPtr = &y;           // yourPtr gets address of y</code>
  
-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.+  * ''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 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.+===== Operators for pointers =====
  
-==== Pointer operators ====+  *  **address operator** ''&'' (already covered). 
 +  *  **indirection** or **dereferencing operator** ''*'' (covered next).
  
-You were introduced to the **address operator**, ''&'', above. The address operator is one of two fundamental pointer operators. The other is the **indirection** or **dereferencing operator**.+===== Indirection/dereferencing operator ===== 
 + 
 +  * **indirection** or **dereferencing** operator, ''*'', accesses the value of what its operand points to.
  
-=== 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''. 
 <code cpp> <code cpp>
 int y = -1;       // declare y and initialize its value int y = -1;       // declare y and initialize its value
 int *myPtr = &y;  // declare pointer and set it to point to y int *myPtr = &y;  // declare pointer and set it to point to y
  
-cout << *myPtr;   // prints -1</code>+cout << *myPtr;   // defererence myPtr (prints -1)</code> 
 + 
 +===== Indirection/dereferencing operator ===== 
 + 
 +  * Dereferencing can be used to assign a value to a location in memory:
  
-The ''*'' operator can be used to assign a value to a location in memory: 
 <code cpp> <code cpp>
 int y = -1;       // declare y and initialize its value int y = -1;       // declare y and initialize its value
Line 181: Line 191:
 cout << y;        // prints 7</code> cout << y;        // prints 7</code>
  
-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:
  
 <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:+===== Indirection/dereferencing operator ===== 
 + 
 +  * ''*'' and ''&'' operators complement each other. 
 +  * The following expressions all evaluate as true: 
 <code cpp> <code cpp>
 *&y == y *&y == y
Line 191: Line 205:
 *&myPtr == myPtr</code> *&myPtr == myPtr</code>
  
-==== Example ====+===== Example =====
  
-The example below demonstrates the use pointers, including address and dereferencing operators. 
 <file cpp pointer-example.cpp>/** Demonstrate basic pointer usage. */ <file cpp pointer-example.cpp>/** Demonstrate basic pointer usage. */
 #include <iostream> #include <iostream>
Line 208: 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 219: Line 231:
          << "&*aPtr = " << &*aPtr << endl          << "&*aPtr = " << &*aPtr << endl
          << "*&aPtr = " << *&aPtr << endl;          << "*&aPtr = " << *&aPtr << endl;
 +
     return 0;     return 0;
 } }
 </file> </file>
  
cplusplus/pointers_1_slides.1553786948.txt.gz · Last modified: 2019/03/28 15:29 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki