C++

Topic: Pointers

Explain the void pointers with example ?

The void type of pointer is a special type of pointer. In C++, void represents the absence of type, so void pointers are pointers that point to a value that has no type (and thus also an undetermined length and undetermined dereference properties).This allows void pointers to point to any data type, from an integer value or a float to a string of characters. But in exchange they have a great limitation: the data pointed by them cannot be directly dereferenced (which is logical, since we have no type to dereference to), and for that reason we will always have to cast the address in the void pointer to some other pointer type that points to a concrete data type before dereferencing it.One of its uses may be to pass generic parameters to a function:// increaser#include <iostream>using namespace std;void increase (void* data, int psize){  if ( psize == sizeof(char) )  { char* pchar; pchar=(char*)data; ++(*pchar); }  else if (psize == sizeof(int) )  { int* pint; pint=(int*)data; ++(*pint); }}int main (){  char a = 'x';  int b = 1602;  increase (&a,sizeof(a));  increase (&b,sizeof(b));  cout << a << ", " << b << endl;  return 0;}output : y, 1603sizeof is an operator integrated in the C++ language that returns the size in bytes of its parameter. For non-dynamic data types this value is a constant. Therefore, for example, sizeof(char) is 1, because char type is one byte long

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 ?