View previous topic :: View next topic |
Author |
Message |
lindsay.wilson.88
Joined: 11 Sep 2024 Posts: 40
|
Ternary/conditional operator - trouble with strings |
Posted: Fri Oct 11, 2024 7:54 pm |
|
|
I'm trying to use the ?: operator to choose a string based on a variable's value and I'm having trouble getting it to return strings. As a basic example, try this:
Code: | int1 clsmode=1; // Clear screen mode, 1 or 0
char teststr[10];
teststr=clsmode ? "Escape" : "Blanks"; |
On the last line, I get an error "Assignment invalid: lvalue is READ ONLY".
I had originally been trying it within a printf:
Code: | printf("Current clear screen mode is %s\r\n", clsmode ? "Escape" : "Blanks"); |
That actually compiles without errors, but produces gibberish when run.
I can get it working with individual characters, for example:
Code: | clsmode ? 'E' : 'B' |
but not with strings. Not the end of the world, I can just use an if statement, but the ternary operator is much neater.
Edit: is this something to do with it?
https://stackoverflow.com/questions/12694202/ternary-operation-with-a-string-output-in-c
Something about being able to initialise a string variable with a string literal but not an expression…. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Sat Oct 12, 2024 12:52 am |
|
|
Yes.
Assuming you have PASS_STRINGS=IN_RAM, then you can do this with
strcpy, but not with an assignment as you show. |
|
|
lindsay.wilson.88
Joined: 11 Sep 2024 Posts: 40
|
|
Posted: Sat Oct 12, 2024 12:16 pm |
|
|
Thanks - yeah, looking around, I think I get a feeling for why it's not possible. Never was entirely happy with pointers!
I've stuck to putting separate printf() in each possible option:
Code: | printf(" 4) Clear screen mode: "); (clsmode) ? printf("Escape codes") : printf("Blank lines"); printf("\r\n"); |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Sun Oct 13, 2024 6:18 am |
|
|
You do realise what I am saying about strcpy?:
Code: |
int1 clsmode=1; // Clear screen mode, 1 or 0
char teststr[10];
strcpy(teststr, clsmode ? "Escape" : "Blanks");
|
Understand that C itself does not understand or handle assignment of a
string with '='. It has to be copied. The '=' form can only be used in
a declared initialisation like:
char teststr[]="something"; |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1354
|
|
Posted: Sun Oct 13, 2024 10:32 am |
|
|
you might also be able to predefine string constants and just pass them into printf using "%s" and a tertiary operator to select which one is passed in. |
|
|
lindsay.wilson.88
Joined: 11 Sep 2024 Posts: 40
|
|
Posted: Sun Oct 13, 2024 11:01 am |
|
|
@Ttelmah Thanks for the suggestion - I've just had a try with using strcpy, and with #device PASS_STRINGS=IN_RAM, but unfortunately it's outputting gibberish. Strcpy works fine with just a single literal, e.g. strcpy(teststr, "Escape");, but as soon as the ternary op is used, it outputs nonsense. Is there something else I should be doing?
I hadn't actually realised that C can't do string assignment with "=" - I probably knew it at one time, but it's been years since I've done anything serious in C.
@jeremiah - Excellent idea, thanks! As it happens, there's quite a lot of other 1-bit parameters which I want to show as "On" / "Off", so this will work perfectly for them. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Sun Oct 13, 2024 11:23 pm |
|
|
That is a limitation of the "pass strings" bodge then. In normal C that
works. I hadn't tested in CCS...
A pity.
Defines are the way to go then. |
|
|
lindsay.wilson.88
Joined: 11 Sep 2024 Posts: 40
|
|
Posted: Mon Oct 14, 2024 7:49 am |
|
|
No problem. I had found a few examples online which should have done the trick, but they again were in normal C. |
|
|
|