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

Conditional Expression operator ?:
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








Conditional Expression operator ?:
PostPosted: Fri Nov 04, 2005 4:29 am     Reply with quote

expr ? expr1 : expr2 should return either expr1 or expr2 depending on expr, correct?

If that is true, then could someone please explain why

printf(lcd_putch, "%d: %s\n", fNum++, i < 16 ? codes0[i] : codes1[i - 16]);

gives a Bad expression syntax error when it is compiled. both, codes0 and codes1 are arrays of strings.

I've tried every combination of surround the expressions with paretheses as well as surrounding the whole statement, but nothing seems to work.

Thanks
Foppie



Joined: 16 Sep 2005
Posts: 138
Location: The Netherlands

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Fri Nov 04, 2005 4:51 am     Reply with quote

try changing %s in %c, you are sending a character, not a string...

further I should personally change i < 16 into (i < 16)
just for the readability

I don't know if this helps., but I hope so...

Good luck
Guest








PostPosted: Fri Nov 04, 2005 5:23 pm     Reply with quote

they are arrays of strings, so I need the %s. I tried various combinations of parethenses (the way you suggested and also around the whole thing), but nothing seems to work.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 04, 2005 5:59 pm     Reply with quote

Quote:
I need the %s.

Older versions of the compiler do not support "%s".
I tested this with PCM vs. 2.734 and it gives an error.
It compiles OK with PCM vs. 3.235.

With older versions of the compiler, you print a string
by passing the name of the array directly to printf(),
as in: printf(array);

So I suggest that you break up your printf statement
into two statements, as shown in the code below.

The following program displays:
Quote:

55: Hello World

Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud = 9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

void lcd_putch(char c)
{
putc(c);
}

//=============================
void main()
{
int fNum;
char array[20];

fNum = 55;
printf(lcd_putch, "%d: ", fNum++);

strcpy(array, "Hello World");
printf(lcd_putch,  array);   // This line displays the array.

while(1);
}
Guest








PostPosted: Fri Nov 04, 2005 6:20 pm     Reply with quote

if I have
printf(lcd_putch, "%d: %s\n", fNum++, codes0[i]);
it works just fine.

I'm fairly certain the problem is in the ?: statement.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 04, 2005 6:22 pm     Reply with quote

Post your compiler version. It's given at the top of the .LST file,
which will be in your project folder. It will be a number like 3.191, etc.
Guest








PostPosted: Fri Nov 04, 2005 6:27 pm     Reply with quote

3.181
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 04, 2005 6:39 pm     Reply with quote

You're going to have to post a test program, because that line
compiles for me with no errors with PCM vs. 3.181.

In your test program, make sure you post all #include, #fuses,
and #use statements, as well as all variable declarations.
Guest








PostPosted: Fri Nov 04, 2005 6:51 pm     Reply with quote

Code:

#device *=16
#include<stdlib.h>

#FUSES XT, NOPROTECT, NOWDT, NOLVP, NOBROWNOUT

#use Delay(clock = 4000000)

#include "lcdDriver.c"
#include "codes.c"

void main()
{
   int i;

   for(i = 0; i < 30; i++)
   {
      printf(lcd_putch, "%d: %s\n", i, i < 16 ? codes0[i] : codes1[i - 16]);
      delay_ms(1000);
   }
}


codes.c contains two arrays of strings (codes0 and codes1).

I appreciate you taking the time to help.
Guest








PostPosted: Fri Nov 04, 2005 6:51 pm     Reply with quote

oops...there should be "#include "16F877A.h"" above all that
Guest








PostPosted: Fri Nov 04, 2005 6:54 pm     Reply with quote

here is the exact error I get:

*** Error 22 "H:\C drive\Desktop\School F05\seniord\PIC\test\displayTest.c" Line 18(68,69): Bad expression syntax -1 is not 0..65535
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 04, 2005 7:05 pm     Reply with quote

In your test program, make sure you post all #include, #fuses,
and #use statements, as well as all variable declarations.
Guest








PostPosted: Fri Nov 04, 2005 7:12 pm     Reply with quote

codes.c was rather long so I made this instead

Code:
#include "16F877A.h"
#device *=16
#include<stdlib.h>

#FUSES XT, NOPROTECT, NOWDT, NOLVP, NOBROWNOUT

#use Delay(clock = 4000000)

#include "lcdDriver.c"
//#include "codes.c"

void main()
{
   const char a[2][5] = {{"1234"}, {"5678"}};

   int i;


   for(i = 0; i < 30; i++)
   {
      printf(lcd_putch, "%d: %s\n", i, i < 16 ? a[0] : a[1]);
      delay_ms(1000);
   }
}
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Fri Nov 04, 2005 7:56 pm     Reply with quote

Reading this post from earlier today might give you some insight into your problem:

http://www.ccsinfo.com/forum/viewtopic.php?t=25028
Guest








PostPosted: Fri Nov 04, 2005 8:20 pm     Reply with quote

ah...apparently it doesn't like the fact that the char array is prefaced with "const". If I take the const out in the example I posted it works. That still leaves me with a problem, though. my codes.c file has the two multi-dimensional char arrays, one of them is 16x12 and the other is 14x12. If I have "const" in front of both of them, I can't use the ?: operator. If I take the "const" out, I get a subscript out of range error.

I guess I'll just have to use an if statement rather than the ?:

Thanks for the help
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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