Advertisement

Array with pointers/adresses

Started by February 22, 2003 11:48 AM
4 comments, last by bilsa 21 years, 8 months ago
Hia! I was wondering how I would create an array with a pointer to an object no matter what object type it is... just simply put an adress in the array? And I would also appreciate if someone could how to define this and dynamicly allocate memory with a "*array = new TYPE;" construction? Thank you very much!
to do this all you need to do is create an array of void pointers. The declaration of that would be

void **MyArray;

and to allocate it with new you would simply do

void **MyArray = new void*[SizeOfArray];

hope that helps but remember that void pointers are an easy way to debug all day =-).
ASCII stupid question, get a stupid ANSI
Advertisement
an array of void* would do the trick.


  // Example#include <iostream>#include <string>using namespace std;int main(){	void* MyArray[10];	string *s = new string("Hello");	MyArray[0] = s;	cout << *(reinterpret_cast <string *> (MyArray[0])) << endl;	cin.get();	delete s;	return 0;}  






DaJero
- Oderint dum metuant
DaJero- Oderint dum metuant
Thank you guys!

What does it mean when you have two ** ... as in void**??
quote: Original post by crafty
What does it mean when you have two ** ... as in void**??
It means pointer-to-a-pointer. It means that MyArray contains an adress that is itself an adress i.e. the adress of the first object in the array. You can do a lot of neat stuff with things like this, but they''re hell to debug

This topic is closed to new replies.

Advertisement