Manipulating Data by Using Pointers
By: Fazal in C++ Tutorials on 2007-09-14
Once a pointer is assigned the address of a variable, you can use that pointer to access the data in that variable. Program below demonstrates how the address of a local variable is assigned to a pointer and how the pointer manipulates the values in that variable.
Manipulating data by using pointers.
1: // Listing 8.2 Using pointers 2: 3: #include <iostream.h> 4: 5: typedef unsigned short int USHORT; 6: int main() 7: { 8: USHORT myAge; // a variable 9: USHORT * pAge = 0; // a pointer 10: myAge = 5; 11: cout << "myAge: " << myAge << "\n"; 12: 13: pAge = &myAge; // assign address of myAge to pAge 14: 15: cout << "*pAge: " << *pAge << "\n\n"; 16: 17: cout << "*pAge = 7\n"; 18: 19: *pAge = 7; // sets myAge to 7 20: 21: cout << "*pAge: " << *pAge << "\n"; 22: cout << "myAge: " << myAge << "\n\n"; 23: 24: 25: cout << "myAge = 9\n"; 26: 27: myAge = 9; 28: 29: cout << "myAge: " << myAge << "\n"; 30: cout << "*pAge: " << *pAge << "\n"; 31: 32: return 0; 33: } Output: myAge: 5 *pAge: 5 *pAge = 7 *pAge: 7 myAge: 7 myAge = 9 myAge: 9 *pAge: 9
Analysis: This program declares
two variables: an unsigned short, myAge, and a pointer to an unsigned
short, pAge. myAge is assigned the value 5 on line 10;
this is verified by the printout in line 11.
On line 13, pAge is assigned the address of myAge. On line 15,
pAge is dereferenced and printed, showing that the value at the address
that pAge stores is the 5 stored in myAge. In line
17, the value 7 is assigned to the variable at the address stored in pAge.
This sets myAge to 7, and the printouts in lines 21-22 confirm
this.
In line 27, the value 9 is assigned to the variable myAge. This value is obtained directly in line 29 and indirectly (by dereferencing pAge) in line 30.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++
Comments