Now I will explain the problem OK.
I have to find a way of calling a function before main.
How can I do it? can i use a pointer?
Give all the possible solutions as soon as you can.
See you soon,
Necra
Why?!
main is, by definition, the start of your program.
IF you would explain what you're actually trying to achieve, people might be able to make relevant suggestions.
How about doing it (whatever "it" is) as part of the startup...?
All Possible Solutions #1 (inclusive).
Learn how the "C" environment works with the Keil product. Especially the Assembly Language constructs.
Hint: Use the simulator, do a reset, and single-step through the code... then find the source file for the code it executed prior to the "void main( void )" location.
Edit it as needed.
--Cpt. Vince Foster 2nd Cannon Place Fort Marcy Park, VA
Will this work ?
int B4main ( void ) { /* some stuff } int dummy = B4main ( ); void main ( void ) { /* do some stuff }
No. - not at all!!
That is really not the way that 'C' programs work!
You really need to get a basic 'C' textbook and understand the fundamentals of the language before moving on to "clever tricks" like this!
NO!
Take a close look and understand the previous answers.
The processor actually runs code from the startup.a51 (or similar) assembly file before the call to main. Look at the map file from the build to see this. look in the startup code to find a LJMP to c_start or _main or something.
Why does it matter if you function is called before the call to main? why not just make it the first call in main? Smells to me like you might be doing it wrong or don't quite understand what you are trying to do.
ND
C++ can call constructors of global variables before the call to main().
C will not call anything before main(), unless the function call is explicitly added somewhere in the startup file(s). Initialized global variables can only be initialized with _constant_ data. A function call is not constant data!
Some compilers have special methods to specify functions to be called before main() - basically a way to create self-initializing lib files, where the link order of the library files controls the order the libraries gets initialized.
But once again: EXACTLY WHY do you need to call a function before main()? The startup files fixes the stack. They initialize variables. They zero memory. They call required C RTL initialization functions.
This works :)
void CallB4other ( void ) { /* some stuff } void main ( void ) { CallB4other ( ); /* do some stuff }
Yey, its not what we asked for but it works OK.
It other words, you really didn't need it to run before main() at all, did you?
All you really needed was to make sure that it was the very first thing in main!
Again, this betrays a fundamental lack of understanding of the basics! You really need to get those foundations solid before you go on - otherwise you are building on quicksand...
void CallB4other ( void ) { /* some stuff }
Avoid the SMS naming convention. It is neither tough, nor practical nor stylish.
Yay,
Thanks for all your responses.
You guys and galls are soooooo clever.