0

I was wondering if there was a way to initialize an array out of a variable from a struct. Say you have a struct like this-

struct Test{  
    int Number;  
};

And you wanted to initialize the int Number to become an array.
I've already tried this, and it doesn't work:

Test t1;  
t1.Number = new int[3];   
t1.Number[3] = 6;

I know ISO C++ forbids resizing arrays, but if there was a way to initialize the integer to be an array, that's not really resizing(isn't it?) Also, vectors don't work inside of structs. I get a "Vector does not name a type" error.

P.S., I can't do this either:

struct Test{  
    int Number[5];  
};

Because at that time I don't know the size of the array I want.

2
  • How could an int magically become an array? It would need to be an int* at the least, but even then you'd best steer clear and look at std::vector<> instead. Commented Mar 31, 2012 at 3:20
  • @ildjarn That's right, it doesn't. That only works if you use a pointer. But I think the 'std::vector<>' approach is a better idea. Thanks Commented Mar 31, 2012 at 3:28

4 Answers 4

2

vector works just fine in structs:

#include <vector>

struct Test {
    std::vector<int> Numbers;
};

I'm not sure what you're really trying to do but I think this comes close.

Sign up to request clarification or add additional context in comments.

Comments

0

One trick to do this

struct Test {
  int Numbers[1];
};

when you initialize the struct, you need to use your own allocation function.

struct Test *NewTest(int sizeOfNumbers) {
  return (struct Test*)malloc(sizeof(struct Test) + sizeof(int)*(sizeOfNumbers - 1));
}

then, you will be able to access the numbers by using,

struct Test *test1 = NewTest(10);
test1->Numbers[0]...
test1->Numbers[1]...
test1->Numbers[9]...

Comments

0

The return value of new int[3] is an int* not an int. To make your code work you can do:

struct Test {  
    int* Number;  
};

int main() {
    Test t1;  
    t1.Number = new int[4]; // Note this should be 4 so that index 3 isn't out of bounds
    t1.Number[3] = 6;
    delete t1.Number;

    return 0;
}

However you should really use a std::vector rather than a static array. Vectors work just fine inside structs:

#include <vector>

struct Test {  
    std::vector<int> Number;  
};

int main() {
    Test t1;  
    t1.Number.resize(4);   
    t1.Number[3] = 6;

    return 0;
}

Comments

0

You can use a pointer to int -- i.e.,

struct Test{  
    int *Number;  
};

Then you can assign this at any future time to point to an array of your preferred size:

t.Number = new int[5];

But as other posters have already said, std::vector, with a small "v", works fine; be sure to #include <vector> so the compiler knows what you're talking about.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.