Need help with an array of a pointer...

Started by
5 comments, last by Jimbo 24 years, 2 months ago
void Yaddayadda(int times) { Blabla *pBlabla[times]; } Why isn''t it possible to compile the code above? The compiler (VC++) just says that it''s not a constant expression (error C2057: expected constant expression).. Is there another way of doing it?
Advertisement
cause you can''t declare an array with a non-constant expression like ''int times''
using a constant or using the new and delete operators are among the easy ways to solve your prob
oops
-werdup-
It should work with this:

void Yaddayadda(int times)
{
Blabla **pBlabla;
pBlabla = new Blabla[times];
}

I guess...
Hmm.. if I compile the code below(inside a class) it compiles fine, but then hangs... Maybe I've missunderstood it?


Blabla **pBlabla; (inside the class)

void Something(int times)
{
*pBlabla = new Blabla[times];
for(int h=0;h!=times;h++)
{
pBlabla[h] = new Blabla(direction,posX,posY);
}
}


Edited by - Jimbo on 3/5/00 6:44:54 AM

Edited by - Jimbo on 3/5/00 6:46:22 AM

Edited by - Jimbo on 3/5/00 6:47:14 AM
...see if this work:

Blabla **pBlabla; (inside the class)

void Something(int times)
{
pBlabla = new (Blabla*)[times];
for(int h=0;h!=times;h++)
{
pBlabla[h] = new Blabla(direction,posX,posY);
}
}


i''m still learning
"after many years of singularity, i'm still searching on the event horizon"
This is how I would do it:

void YaddaYadda( int times ){    // create a pointer to a  pointer, essentially a pointer to an array of pointers    BlaBla **pBlabla = 0;        // create the first array.    pBlabla = new BlaBla* [ times ];    // you now have an array of BlaBla pointers.    // fill them with whatever you want.    // NOTE: They are un-initialised, stray pointers.}


I hate non-dynamic arrays

===============================================
I saw a man pursuing the horizon;
Round and round they sped. I was disturbed at this; I accosted the man.
"It is futile," I said, You can never -- "

"You lie," he cried, And ran on.
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.

This topic is closed to new replies.

Advertisement