Problem assigning a string constant to a variable
Hi all, I've stumbled across an assignment problem and I'm wondering if anybody could explain what's happening.
Include file:
Main file:
This results in a compiler warning:
I can't for the life of me figure out why it wants to cast that to a CHAR. Naturally it works fine if I copy-and-paste the string in place of the constant. Does anybody have any insights into this? Thanks.
Include file:
DEFINE_CONSTANT char SWITCH_PORT_1[] = '1.3.6.1.4.1.1.2.3.4.5.0';
Main file:
DEFINE_VARIABLE CHAR sSwitchOID[] = SWITCH_PORT_1;
This results in a compiler warning:
WARNING: Main.axs(75): C10571: Converting type [string] to [CHAR]
I can't for the life of me figure out why it wants to cast that to a CHAR. Naturally it works fine if I copy-and-paste the string in place of the constant. Does anybody have any insights into this? Thanks.

0
Comments
Try this.
DEFINE_CONSTANT
Char CONST_TMP [] = '1.2.3.4.5.6.7.8.9.0';
DEFINE_VARIABLE
VOLATILE Char TmpStr [50];
DEFINE_START
{
TmpStr = "CONST_TMP";
}
I will try in the am when I get into the office.
Paul
jdonachiue: Setting it in DEFINE_START works fine like you suggest. That will be my Plan B. I'm still curious to know what's at work here though!
a_riot42: I tried creating a fresh workspace with only the following in Main.axs:
I still get the warning "WARNING: C:\Code\TestWorkspace\Main.axs(8): C10571: Converting type [string] to [CHAR]".
Did you do anything differently?
into my current project and it compiles with no errors.
Paul
Thanks for testing, Paul!
Paul
Ah, that is the kind of insight I was looking for. Thanks.
Not sure what you mean. It exists at compile time, since its hard coded in the file. Its read only, but that doesn't mean you can't assign it to a variable.
It looks to me like he forgot the second set of brackets. This compiles with no warnings.
char sSwitchOID[][] = SWITCH_PORT_1;
Paul
If I follow Paul's suggestion I get no warning. But when I run this code on an NI-700... ...I get the following in my diagnostic log: If I add a constant declaration: Then I still get no warnings, but now diagnostic output like this: "Forgot" is too kind.
You're coloring outside the lines, so you're on your own here. Once embarked on a Netlinx gremlin finding exercise, you never know what'll turn up. What gets printed if you add a third set of brackets?
char sSwitchOID[][][] = SWITCH_PORT_1;
Paul
With the "constant" modifier I can have up to 502 pairs of square brackets and get the same behaviour: variable set correctly, reported length 23.
If I exceed either of those limits I get an error: This happens to be slightly over 1024 bytes in a line so it I suspect it is a parser error than anything else.
Obviously this example is ridiculous, but arbitrary behaviour from the compiler makes me unhappy. Perhaps somewhere there is a note "The behaviour when initialising a global variable with a constant defined under DEFINE_CONSTANT is undefined", though I haven't spotted it in any documentation. I think I'll just make that a personal rule and move on.
Thanks all for the suggestions!
Interesting. I wonder how much memory a 503 dimensional array takes up? I shudder to think.
Then you are bound to be an unhappy Netlinx programmer. This is the AMX jungle not the clean clinical labs of Sun Microsystems. Be grateful that at least the compiler gave you a warning! Often you just get runtime weirdness with nothing to indicate where to even begin looking. Remember segmentation faults where your OS would simply crash when you ran your program? No fancy shmancy null pointer messages with the accompanying stack trace here. You need to practise defensive programming in Netlinx, which means don't do anything unless you know for sure it will work in the real world. If you start to assume the compiler works correctly or the documentation is always correct, you will end up with a lot of gray hair!
Paul
Haha okay, useful advice. Thanks.