1

Each time the function is being called, i want to create a new array with a new name to save the results and later compare different arrays to check if they are the same. I want the arrays being created with different but uniformed names each time like array1, array2.... And will "static" keyword work because these arrays need to remain in the memory after the function returned.

something like this

func()
{static char array1[10];
 .......
}
3
  • 3
    What about std::vector? It does the job for you Commented Nov 6, 2014 at 13:19
  • 1
    in this context static means that your array isn't allocated on the stack (ie, when func is called), but rather in the data section of your program, but with visibility limited to func. The feature you describe isn't possible in C++ that way. Commented Nov 6, 2014 at 13:19
  • Now here's the important question: how do you plan to use these arrays? Do you wish to access them by their names in the code, eg. array9[5] = 3; ? Or do you merely wish to check if they exist? If the later, you will need some sort of container to store and index them. Commented Nov 6, 2014 at 13:23

1 Answer 1

4

Use a static vector of arrays:

void func()
{
    using array_type = std::array<char,10>;
    static std::vector<array_type> store;

    array_type your_array;
    // ... fill your_array
    store.push_back(your_array);
}
Sign up to request clarification or add additional context in comments.

2 Comments

You don't have to put a space in between > > anymore in C++11 and above.
sure, but that's one thing I'll continue also in C++11. It doesn't hurt, and you get slightly more pre-C++11 compability (e.g. if one decides to change the array above into a vector and switch to an old compiler, that is a thing you don't have to worry about...)

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.