Same as accessing the value at the memory address held by pointer by indirection, the indirection can also be used to manipulate variable's value. Assigning a value to a dereferenced pointer will indirectly change a value of a variable the pointer is pointing to. The following example illustrates simple manipulation of data with pointers:#include <iostream>int main(){ using namespace std;// Declare an integer variable and initialize it with 99 unsigned short int myInt = 99;// Declare and initialize a pointer unsigned short int * pMark = 0;// Print out a value of myInt cout << myInt << endl;// Use address-of operator & to assign memory address of myInt to a pointer pMark = &myInt;// Dereference a pMark pointer with dereference operator * and set new value *pMark = 11;// show indirectly a value of pMark and directly the value of myInt cout << "*pMark:\t" << *pMark << "\nmyInt:\t" << myInt << endl;return 0;}OUTPUT:99*pMark: 11myInt: 11Dereferencing a pMark pointer and assignment of new value to 11 will indirectly alter a value of myInt also to 11. It is important to bring up again that pointer holds only a variable's memory address not its value. To access a value of the variable to which a pointer is pointing to the pointer must be derefereced by dereference operator “ * “. Note “ * “ is not a multiplication, by the context of your C++ code your compiler will differentiate if your intention is to use multiplication or dereference operator.
C++
Topic: Pointers
Explain about Manipulating Data with Pointers and example ?
Browse random answers:
What is Pointers in c++ ?
What is importance of const. pointer in copy constructor?
Explain "passing by value", "passing by pointer" and "passing by reference"
Where we use reference and where we use pointers ?Write their differences and advantages and disadvantages .
Explain which of the following declarations will compile and what will be constant - a pointer or the value pointed at: * const char ** char const ** char * const
Why Pointers are not used in C Language. What are the main differences between C and C++
Explain about Reference operator (&) ?
Explain about declaring variables of pointer types with example ?
Explain about pointers and arrays ?
Explain about Pointer initialization ?
Explain about Pointer arithmetics ?
Explain about Pointers to pointers ?
Explain the void pointers with example ?
What is Null pointer ?
What is Pointers to functions ?
What Are Pointers?
What are Using Pointers in C++ ?
Explain Accessing the Value at the Memory Address held by a Pointer ?
Why Use Pointers?
What are C++ Pointer Syntax ?
Explain about Taking Stock of Pointers ?
What are pointers? Why should you care?
Explain about Manipulating Data with Pointers and example ?
What are Array of pointers ?
Where do we use pointers?
Why do we need pointers?
Allocating a memory from the "Free Store" ?
Explain about Allocating a memory with new operator ?
Explain about De-allocating a memory with delete operator ?
Explain about Data type class pointers with example ?
Explain about memory leaks associated with pointers ?
Explain about memory leak caused by pointer reassignment ?
When local variable’s name is same as member’s name
What are function Pointer Syntax ?
What are Reading Function Pointer Declarations ?
What are Using a Function Pointer ?
What are Function Pointers in the Wild ?
How to Using Polymorphism and Virtual Functions Instead of Function Pointers (C++) ?
What are Benefits of Function Pointers ?
Where They get Useful Function Arguments ?
Explain Smart Pointers (Modern C++) ?
What are Uses for smart pointers ?
What are Kinds of Smart Pointers ?
What are another Use of Pointers Efficient Array Processing ?
Explain about C++ Pointers and new
What are problems with pointers ?
Explain about generic smart pointer class ?
How to initialize a vector of pointers?
Explain about c++ Pointer Arithmetic ?
Explain about incrementing a Pointer ?
Explain decrementing a Pointer ?
Explain pointer Comparisons ?