Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating and Using Pointer Variables in C++ By: Ed Brunjes

Similar presentations


Presentation on theme: "Creating and Using Pointer Variables in C++ By: Ed Brunjes"— Presentation transcript:

1 Creating and Using Pointer Variables in C++ By: Ed Brunjes

2 What are Pointer Variables?
Data as it is stored in a computer has two components The information or data The beginning location at which the data is “stored” The pointer variable contains the address of the data; NOT the actual data Pointer variables may be used for ANY data type. Integers are used here as an example.

3 Two operators associated with pointers
& - Means “address of…” and is generally used with standard variables to extract the beginning address of the data. * - With one exception, is read “contents of…” and refers to the data stored at the address in the pointer

4 The Process of Creating a Pointer Variable
The creation is a two-step process – Creation of the variable Allocation of memory for the data and the assignment of the beginning address of the computer memory used to the pointer variable

5 The syntax of pointer variable creation
First the pointer variable is created – first the data type that is to be “pointed to” then the asterisk followed by the pointer variable name and semi-colon. This is the one exception to “contents of …” indicated above. int *iptr;

6 The syntax of pointer variable creation (cont’d)
Next memory is allocated for the data with the beginning address being returned and assigned to the pointer variable. new – is the operator that allocates memory for data (amount of memory is determined by the requirement of the trailing data type) and returns the beginning address. iptr = new int; Note that the variable creation and allocation may occur at different locations in the program.

7 The syntax of pointer variable creation (cont’d)
An alternate syntax- Sometimes the creation of the pointer variable and allocation of memory are required together. If this occurs we can combine the processes together as follows – int *iptr; iptr = new int; Can be replaced by – int *iptr = new int;

8 The syntax of pointer variable creation (cont’d)
The two operations together – int *iptr; iptr = new int; Note that NO data has been assigned at this time

9 Assigning data to a pointer variable
Consider the following (it assumes the previous declaration and allocation) *iptr = 25; The statement is read, “the contents of (the memory location in) iptr gets 25. Note – iptr does NOT get the 25, but the memory location in iptr gets the 25.

10 Assigning data to a pointer variable (cont’d)
The follow diagram is after the following statement is executed. *iptr = 25;

11 Assigning data to a pointer variable (cont’d)
At this point the pointer variable with the asterisk acts like a standard variable of the given type and has the same limitations. The biggest difference is that now we can manipulate the address separately from the data. With that ability we must be careful that we do not loose the address of the data which would mean we loose the data.

12 Using pointer variables as synonyms for data in standard variables
Sometimes it is necessary to create synonyms for data in standard variables so the data might be used and manipulated directly in other modules (functions) in the program. This calls for the use of a pointer variable to receive the address of a standard variable (“&”).

13 Consider the following –
Using pointer variables as synonyms for data in standard variables (cont’d) Consider the following – int ivar = 25; int diffVar; int *iptr; // diffVar = ivar; cout << ivar; cout << diffVar; Each of the two output lines will output 25, however each line will be a different 25!

14 Using pointer variables as synonyms for data in standard variables (cont’d)
If we modify the data in one variable what will happen if we print the other? ivar = 47; cout << diffVar; We get the 25 printed – why? Now consider - iptr = &ivar; ivar = -14; cout << *iptr; What is output? Yes, the -14!!!! Because iptr has the address of ivar. When we print the “contents of iptr” we get the value associated with ivar!!

15 Creating and Using Pointer Variables in C++
Conclusions There are 3 operators typically used with pointer variables - *, &, new. Creating a pointer is a 2-step process – creating the pointer variable and allocating memory for data using the new operator. Assigning data to the location pointed to by a pointer requires the use of an * (“contents of …”).


Download ppt "Creating and Using Pointer Variables in C++ By: Ed Brunjes"

Similar presentations


Ads by Google