Index

C & C++ Programming - Void Pointer

#include "stdafx.h"

class CVoidPointer
{
public:
 int m_nInteger;
 char m_cCharacter[2];
};

void main()
{
 void *Ptr[2];
 CVoidPointer objVoidPointer;

 objVoidPointer.m_cCharacter[0] = 'A';
 objVoidPointer.m_cCharacter[1] = '\0';
 objVoidPointer.m_nInteger = 10;

 Ptr[0] = objVoidPointer.m_cCharacter;
 // Ptr[0] = &objVoidPointer.m_cCharacter; same as above
 Ptr[1] = &objVoidPointer.m_nInteger;
 // Ptr[1] = &(objVoidPointer.m_nInteger); same as above

 cout << "Printing with object" << endl;
 cout << objVoidPointer.m_cCharacter << endl;
 cout << objVoidPointer.m_nInteger << endl;

 cout << "Printing with void pointer" << endl;
 cout << (char *) Ptr[0] << endl;
 cout << *((int *) Ptr[1]) << endl;
}
Index