Archived

This topic is now archived and is closed to further replies.

Sky

Help with arrays...

Recommended Posts

Here''s what i''m trying to do... float array[3]; and then later in the program just define all the variables in the array at once... array[] = {1,2,3}; What is the proper syntax for this? Or is it even possible? i know you can define them all when you initialize the array, But I can''t find any information about how to/if you can do it later in the program...

Share this post


Link to post
Share on other sites
The syntax you are trying to use is only valid during the initial definition of the array. Once the array has been defined, you can only assign individual elements.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
Here''s some homework for you : try to either find a technical reason why you cannot do it or how you could modify a compiler to let you do it while keeping your arrays binary-compatible with C.

It''s very instructive.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
I don''t see any reason why the compiler wouldn''t be able to do it. I mean I could easily make a function to do the exact same thing... (except for the fact that I don''t know how to pass array''s through functions and I don''t know nearly enough to make it so that just doing like array[whatever] = {33,43,3,1,2} would work. (though if I new more commands I think I would be able too...))

Share this post


Link to post
Share on other sites
This is a crappy solution to your problem, but you could just do the initial definition of your array after you have the values you want to insert. You don''t have declare your array at the start of a function it''s just good coding practice.

Share this post


Link to post
Share on other sites