Initialization of LOCAL_VAR
microchip78
Posts: 25
Hi Experts,
I am new to AMX Programming.
I tried to define a variable as LOCAL_VAR inside the function and try to initialize it.
But NetLinx Studio prompt me an error saying that you can only initialize variable inside DEFINE_VARIABLES and DEFINE_CONSTANTS only ...
But NetLinx Language Reference Guide says that you can initialize the variable ... Check pages 11, 46, 47, 58 of the book and you will find the references for LOCAL_VAR. But when I tried to do that way Netlinx Compiler gave me an error C10213: Illegal Initialization for symbol ....
Can anyone help me with this ?
Regards,
MC78
I am new to AMX Programming.
I tried to define a variable as LOCAL_VAR inside the function and try to initialize it.
But NetLinx Studio prompt me an error saying that you can only initialize variable inside DEFINE_VARIABLES and DEFINE_CONSTANTS only ...
But NetLinx Language Reference Guide says that you can initialize the variable ... Check pages 11, 46, 47, 58 of the book and you will find the references for LOCAL_VAR. But when I tried to do that way Netlinx Compiler gave me an error C10213: Illegal Initialization for symbol ....
Can anyone help me with this ?
Regards,
MC78
0
Comments
For example:
Thanks for your response ...
I understand you point that outside the DEFINE_VARIABLES and DEFINE_CONSTANTS you just put
But then it will not serve the purpose of LOCAL_VAR (Static Variable)
Reason of LOCAL_VAR is, I want to retain its value till next use ... and as soon as it hit th MY_VAR = 1 it will reload with 1 ... and lost its previous value ...
So that doesnt serve the purpose of LOCAL_VAR ...
Please let me know if I am understanding incorrectly ...
I think I understand what you mean ....
so if you dont assign it, it will have a value 0 or 0.0. But what if I want to assign local variable with 5.
Or what if the local variable is string (char array) and I want to assign some string to it ...
Thanks in advance ...
If that won't work, create a separate local_var that indicates whether the variable has been initialized or not. If it's zero, initialize my_var and set the flag to 1.
Thanks for your reply ...
Finally I used second solution suggested by you ... use a variable as a flag to indicate whether its been used or not ...
Thanks anyway for your responses ...
Paul
If possible I'll use a stack but if I need to retain a value I'll use a local and if I need to access the var from various locations I'll go global.
I agree with vining, and follow the "principle of least scope", so that each variable has the smallest scope possible. Generally my only global variables are for overall system state (projector power, etc.) and polling loops in DEFINE_PROGRAM. That way you don't forget and reuse "TEMPJ" in a function that gets called from another function using "TEMPJ" and wipe it out. Temporary/loop variables are always STACK_VARs.
Jeremy
My point wasn't that you should always use globals, that would be silly. I always prefer to use the stack rather than the heap whenever possible. It was more that if you find yourself using a lot of static variables in functions then it may be wise to look at your design and see if it can be written without static variables at all. They have their usefulness but I have found them to be abused in the netlinx language.