on line 239 before the final return DOES successfully give the right output. So something appears to be going wrong in the way the CrncyFmt function is being called by the main function or the way the CharOut is returned.
Not here it doesn't. In C++, undeclared externals are a flat-out error. At the very least it should be a warning, which you the programmer should treat as an error because using undeclared externals indeed mess up very badly on many platforms.
Ahah... When your function returns, the object is no longer in memory, so the memory the string was stored in is no longer allocated and may be overwritten with garbage at any time. It's a sneaky form of returning a pointer to a local variable, disguised enough that the compiler can't warn you about it.
You should pass a buffer into the function to copy the string into instead.
Thanks again for both the note on undeclared externals (still not sure why I get no warning or error) and the solution.
Unfortunately I need my CrncyFmt function to return a Char*, not be void as it will eventually be run standalone.
Alternatively if it makes sense to make CrncyFmt a wrapper function with the original signature it had and put the majority of the code into a void function that would be fine too. Will that work?
Lee
---------- Post updated at 04:20 PM ---------- Previous update was at 04:15 PM ----------
Ok, so seeing your code, I decide to try the following as the final lines:
strcpy(CharIn, CharOut);
return CharIn;
Which works. Is it going to cause any issues to overwrite the input value like that?
Yes. You're overwriting not just your input but all kinds of data beyond it you shouldn't be, corrupting main()'s argument array.
If you really, really need to return a char * (why can't you pass a buffer into a standalone? everything else does, that's how it's supposed to work) you can return a pointer to a static local variable.
The reason I need to return a char, not void, is because it's run from within Datastage, which expects functions to have a return value. So for example a derivation for an output will be CrncyFmt("123.456","2"). There is no option to specify void functions.
"CrncyFmt.cpp", line 63.24: 1540-0258 (S) A return value of type "char *" cannot be initialized with an expression of type "const char *".
Adding Const to the function return type and in the main function worked but then still returned an empty string from main. The environment is not fully functional yet so I can't test this from within Datastage, perhaps it would work.
Corona, thanks for your reply and help, unfortunately my almost non-existent C++ knowledge means I'm sure I've misunderstood you and am not doing it right.
Achenle: I tried your solution and it seems to work fine. Have not yet been able to test from within Datastage yet, but thanks very much for you help!
Don't apologize, I don't expect you to know everything. But I can't correct your code if I can't see it from here either! You have to post it!
Will Datastage be able to free() the resulting memory after? if not, your application will leak memory. I suspect it would not, hence why I was suggesting static local variables instead.
Thanks again for getting back to me. My final code is as achenle suggested: as per my original attachment with that single change. This seems to work fine and I have checked the Datastage forums and verified that it handles memory allocation to ensure no leaks.