C++

Topic: Pointers

Explain about declaring variables of pointer types with example ?

Due to the ability of a pointer to directly refer to the value that it points to, it becomes necessary to specify in its declaration which data type a pointer is going to point to. It is not the same thing to point to a char as to point to an int or a float.The declaration of pointers follows this format:type * name;where type is the data type of the value that the pointer is intended to point to. This type is not the type of the pointer itself! but the type of the data the pointer points to. For example:int * number;char * character;float * greatnumber;These are three declarations of pointers. Each one is intended to point to a different data type, but in fact all of them are pointers and all of them will occupy the same amount of space in memory (the size in memory of a pointer depends on the platform where the code is going to run). Nevertheless, the data to which they point to do not occupy the same amount of space nor are of the same type: the first one points to an int, the second one to a char and the last one to a float. Therefore, although these three example variables are all of them pointers which occupy the same size in memory, they are said to have different types: int*, char* and float* respectively, depending on the type they point to.I want to emphasize that the asterisk sign (*) that we use when declaring a pointer only means that it is a pointer (it is part of its type compound specifier), and should not be confused with the dereference operator that we have seen a bit earlier, but which is also written with an asterisk (*). They are simply two different things represented with the same sign.Now have a look at this code:// my first pointer#include <iostream>using namespace std;int main (){  int firstvalue, secondvalue;  int * mypointer;  mypointer = &firstvalue;  *mypointer = 10;  mypointer = &secondvalue;  *mypointer = 20;  cout << "firstvalue is " << firstvalue << endl;  cout << "secondvalue is " << secondvalue << endl;  return 0;}firstvalue is 10secondvalue is 20Notice that even though we have never directly set a value to either firstvalue or secondvalue, both end up with a value set indirectly through the use of mypointer. This is the procedure:First, we have assigned as value of mypointer a reference to firstvalue using the reference operator (&). And then we have assigned the value 10 to the memory location pointed by mypointer, that because at this moment is pointing to the memory location of firstvalue, this in fact modifies the value of firstvalue.In order to demonstrate that a pointer may take several different values during the same program I have repeated the process with secondvalue and that same pointer, mypointer.Here is an example a little bit more elaborated:// more pointers#include <iostream>using namespace std;int main (){  int firstvalue = 5, secondvalue = 15;  int * p1, * p2;  p1 = &firstvalue;  // p1 = address of firstvalue  p2 = &secondvalue; // p2 = address of secondvalue  *p1 = 10;          // value pointed by p1 = 10  *p2 = *p1;         // value pointed by p2 = value pointed by p1  p1 = p2;           // p1 = p2 (value of pointer is copied)  *p1 = 20;          // value pointed by p1 = 20    cout << "firstvalue is " << firstvalue << endl;  cout << "secondvalue is " << secondvalue << endl;  return 0;}firstvalue is 10secondvalue is 20I have included as a comment on each line how the code can be read: ampersand (&) as "address of" and asterisk (*) as "value pointed by".Notice that there are expressions with pointers p1 and p2, both with and without dereference operator (*). The meaning of an expression using the dereference operator (*) is very different from one that does not: When this operator precedes the pointer name, the expression refers to the value being pointed, while when a pointer name appears without this operator, it refers to the value of the pointer itself (i.e. the address of what the pointer is pointing to).Another thing that may call your attention is the line: int * p1, * p2;This declares the two pointers used in the previous example. But notice that there is an asterisk (*) for each pointer, in order for both to have type int* (pointer to int).Otherwise, the type for the second variable declared in that line would have been int (and not int*) because of precedence relationships. If we had written:int * p1, p2;p1 would indeed have int* type, but p2 would have type int (spaces do not matter at all for this purpose). This is due to operator precedence rules. But anyway, simply remembering that you have to put one asterisk per pointer is enough for most pointer users.

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 ?