I first posted this here
http://www.cplusplus.com/forum/beginner/45574/ but I think it might have been in the wrong section, and maybe I didn't explain it very well so I thought I'd rephrase it here with a bit more detail.
Say I have a function,
get_global()
that carries out some operations and then returns a value that I will declare as a
const int MYGLOBAL
. Now, I also want to define a structure called
mystruct
, whose members will be arrays whose lengths are defined by the
MYGLOBAL
. and another function called
fun_build_mystruct()
which will return a structure member of type
mystruct
. Conceptually, I would do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include<headers.h>
const int get_global() {
// define my function here
return MYGLOBAL;
}
const int MYGLOBAL = get_global(); // Can't do this! - we're not in main()
struct mystruct {
int structarray [MYGLOBAL] // this array size is dependant on the value returned by get_global()
};
mystruct * fun_build_mystruct() {
mystruct MEMBER;
return MEMBER;
}
main () {
//do stuff
return 0;
}
|
This however, doesn't work because I can't call
get_global()
outside of main(). But I can't define my functions inside main, and I can't even declare prototypes because fun_build_mystruct() needs mystruct to be defined before it can be declared. How is this sort of thing normally handled??