Discussion:
structure
(too old to reply)
sa
2008-06-12 06:05:49 UTC
Permalink
Hello all.

I am trying to create structured array dynamically.
I am getting this error message "const expression expected."
struct udtConfigLabel
{
String present,link,last_modified;
WideString title;
};

udtConfigLabel *Lang[3];
Lang[0] = new udtConfigLabel[10];
Lang[1] = new udtConfigLabel[10];
Lang[2] = new udtConfigLabel[10];

It is possible to create the pointer array dynamically, like
int size = //size giving at runtime.
udtConfigLabel *Lang[size];

Kind Regards
SA
Chris Uzdavinis (TeamB)
2008-06-11 18:31:47 UTC
Permalink
Post by sa
Hello all.
I am trying to create structured array dynamically.
I am getting this error message "const expression expected."
struct udtConfigLabel
{
String present,link,last_modified;
WideString title;
};
udtConfigLabel *Lang[3];
Lang[0] = new udtConfigLabel[10];
Lang[1] = new udtConfigLabel[10];
Lang[2] = new udtConfigLabel[10];
It is possible to create the pointer array dynamically, like
int size = //size giving at runtime.
udtConfigLabel *Lang[size];
You can use std::vector<utdConfigLabel>:


//#include ...
#include <vector>

struct udtConfigLabel
{
String present,link,last_modified;
WideString title;
};

typedef std::vector<utdConfigLabel*> ConfigLabels;

int main()
{
int size = get_runtime_size();
ConfigLabels Lang(size);

// now you can access Lang[0] ... Lang[size-1]

// to add even more, use push_back:
// Lang.push_back(new udtConfigLabel[10]);
}


Note, I really don't like having raw pointers laying around like this,
since it's so easy to get wrong and leak, or accidently delete when
you shouldn't, or use delete instead of delete[], etc.

Instead, you should use a vector of smart pointers, or a vector of
vectors.
--
Chris (TeamB);
Remy Lebeau (TeamB)
2008-06-11 18:42:36 UTC
Permalink
Post by sa
It is possible to create the pointer array dynamically, like
int size = //size giving at runtime.
udtConfigLabel *Lang[size];
Not like that, no. You have to use 'new[]', like any other dynamically
allocated memory:

udtConfigLabel** Lang = new udtConfigLabel*[size];
Lang[0] = new udtConfigLabel[10];
Lang[1] = new udtConfigLabel[10];
Lang[2] = new udtConfigLabel[10];
...
delete[] Lang[0];
delete[] Lang[1];
delete[] Lang[2];
...
delete[] Lang;


Gambit

Loading...