Wednesday, 18 April 2012

Mention the conditions in which we should use references and pointers separately. | C++

While creating a pointer, you need to declare a pointer variable and then store the address of another variable in it. A pointer variable can directly access the value stored in the other variable by using the * operator, as shown in the following code snippet:
cout<<*ptr;
In the preceding code snippet, the * operator informs the compiler to access and display the value stored at the memory location referenced by the ptr pointer variable.
The * operator is also known as the dereference operator. The dereference operator (*) accesses the data stored in the variable pointed by the pointer variable.
The reference variable is generally used to simplify the dereferencing of the complex objects, which are found inside arrays, structures, arrays of structures, and array inside structures.

No comments: