-1

I am wondering if this is possible (for very easy use in project)

Say:

// array size (COUNT)
int foo[COUNT];

// values
foo[0] = 1;
foo[1] = 43;
foo[2] = 24;

// define (or equivalent) its size at the end
#define COUNT 3    

(This is by design, so I don't have to fine tune it whenever I change the arrays length)

Thanks.

Edit: What I am looking for is defining the size of the array after it has been filled with values. In the example I only know that it is 3 when I put values. So I could add 4 more "foo"s and only need to change the #define below.

Next Edit:

// this is the idea, can this be possible? or even a "forward" declared
int foobar = THEVALUE

// way further down
#define THEVALUE 5;
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Evan Carslake
  • 2,267
  • 15
  • 38
  • 56
  • 1
    Okay, I voted to close and then got what you were asking. Anyway, the answer is no. Preprocessor macros are simple text substitution. You can't substitute what isn't defined yet. – StoryTeller - Unslander Monica Dec 09 '15 at 01:01
  • @StoryTeller yeah that is what I want. If the macros were parsed in reverse, there would not be an issue. I have the array declared at the very start of my code, and way towards the end I populate it. – Evan Carslake Dec 09 '15 at 01:27
  • 1
    @StoryTeller: You can reverse the close vote. – Cheers and hth. - Alf Dec 09 '15 at 01:28
  • 1
    AFAIK there is no common tool-chain that supports what you are asking. In order to achieve it you'll need to find or create a C++ compliant preprocessor and use it in your build process instead of the one included with the tool chain. There's a good reason why this is not an available option - it's a _terrible_ idea! – Captain Obvlious Dec 09 '15 at 01:45
  • Can you explain _why_ you need this? Seems like an XY problem. – Emil Laine Dec 09 '15 at 01:47
  • @zenith Yeah, thinking it over, it is a bad idea and should just do the "standard" this is too "out of the box" thinking. – Evan Carslake Dec 09 '15 at 01:53
  • @Cheersandhth.-Alf, thanks. I have no idea how I missed the retract vote button this entire time -_- – StoryTeller - Unslander Monica Dec 09 '15 at 09:47

2 Answers2

3
int foo[] = {1, 43, 24};
int const count = 3;    // See the SO array FAQ for how to compute this.

A simple type safe way to compute size, not mentioned in the SO array FAQ (because it was written before C++11), is

int const count = end( foo ) - begin( foo );

where end and begin are the std namespace functions from the <iterator> header.

See the SO array FAQ for other ways.


Generally, in modern C++ it is preferable to use std::array (fixed size) and std::vector (dynamic size) over raw arrays. This is safer and with more rich functionality, including in particular assignment and the ability to check the size easily. Unfortunately std::array does not support a size inferred from the initializer, so in this you'd have to use std::vector even if the array size is meant to be constant:

vector<int> foo = {1, 43, 24};

// foo.size() gives you the size at any moment.
Community
  • 1
  • 1
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 3
    Also, C++17 will have `std::size` as a free function. – Brian Bi Dec 09 '15 at 01:14
  • @Cheersandhth.-Alf slightly what I want. I edited my first post, the last example shows exactly what I am thinking... – Evan Carslake Dec 09 '15 at 01:32
  • 1
    @EvanCarslake: I find it difficult to grok what you're really after. It's clearly an XY question: having a real problem X, envisioning an impractical Y as the solution, asking about Y, and not mentioning X. So what is X in your case. Is it, that you want to declare the array, fill it with values, and then be able to refer to its current size? That's what `std::vector` does for you. – Cheers and hth. - Alf Dec 09 '15 at 01:39
1

You could initialize array with initializer list, then you don't need to know its size at all:

int foo[] = { 1, 43, 24 }
int size = sizeof(foo) / sizeof(int); // if you do need to know size

EDIT: For a more idiomatic C++11 see the answer above :)

Eugene
  • 7,180
  • 1
  • 29
  • 36