How to define an array in a structure?
How to define an array in a structure?
1 2 3 4
|
STRUCT A
{
B data[];
};
|
why is this wrong?
try a vector
Because:
(a) the C++ keyword is
struct
, not
STRUCT
.
(b) you need to declare the array with its size.
But, as DTSCode says:
Vectors are much safer than C-style arrays.
Last edited on
Well...c++ is much more complicated than c...
Thanks ...
a friend of mine told me to try defining a pointer in the structure like this:
1 2 3
|
struct A {
B* pdata;
};
|
and
1 2
|
A a;
a.pData = new B[100];
|
Is this solution better than vector?
Last edited on
no. well its c++ but then you have to remember to delete it and you still wont have as much as a vector will give you
well..i failed..
syntax error : missing ';' before '*'
1 2 3 4 5 6
|
struct A {
B* pdata;
};
struct B{
int i;
};
|
This won't work. I got an error...
but i changed it into
1 2 3
|
struct A {
char* pdata;
};
|
Then it can be compiled successfully...
why can't the first one be compiled? any syntax error?
Because you're trying to use the type
B before you've declared it.
Since you're only using a pointer to B, you can use a forward declaration:
1 2 3 4 5 6 7 8 9
|
struct B;
struct A {
B* pdata;
};
struct B{
int i;
};
|
Last edited on
Topic archived. No new replies allowed.