CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Ternary/conditional operator - trouble with strings

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
lindsay.wilson.88



Joined: 11 Sep 2024
Posts: 40

View user's profile Send private message

Ternary/conditional operator - trouble with strings
PostPosted: Fri Oct 11, 2024 7:54 pm     Reply with quote

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: 19529

View user's profile Send private message

PostPosted: Sat Oct 12, 2024 12:52 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Oct 12, 2024 12:16 pm     Reply with quote

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: 19529

View user's profile Send private message

PostPosted: Sun Oct 13, 2024 6:18 am     Reply with quote

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: 1351

View user's profile Send private message

PostPosted: Sun Oct 13, 2024 10:32 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Oct 13, 2024 11:01 am     Reply with quote

@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: 19529

View user's profile Send private message

PostPosted: Sun Oct 13, 2024 11:23 pm     Reply with quote

That is a limitation of the "pass strings" bodge then. In normal C that
works. I hadn't tested in CCS... Sad
A pity.

Defines are the way to go then. Very Happy
lindsay.wilson.88



Joined: 11 Sep 2024
Posts: 40

View user's profile Send private message

PostPosted: Mon Oct 14, 2024 7:49 am     Reply with quote

No problem. I had found a few examples online which should have done the trick, but they again were in normal C.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group