|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Conditional Expression operator ?: |
Posted: Fri Nov 04, 2005 4:29 am |
|
|
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
|
|
Posted: Fri Nov 04, 2005 4:51 am |
|
|
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
|
|
Posted: Fri Nov 04, 2005 5:23 pm |
|
|
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
|
|
Posted: Fri Nov 04, 2005 5:59 pm |
|
|
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:
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
|
|
Posted: Fri Nov 04, 2005 6:20 pm |
|
|
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
|
|
Posted: Fri Nov 04, 2005 6:22 pm |
|
|
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
|
|
Posted: Fri Nov 04, 2005 6:27 pm |
|
|
3.181 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 04, 2005 6:39 pm |
|
|
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
|
|
Posted: Fri Nov 04, 2005 6:51 pm |
|
|
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
|
|
Posted: Fri Nov 04, 2005 6:51 pm |
|
|
oops...there should be "#include "16F877A.h"" above all that |
|
|
Guest
|
|
Posted: Fri Nov 04, 2005 6:54 pm |
|
|
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
|
|
Posted: Fri Nov 04, 2005 7:05 pm |
|
|
In your test program, make sure you post all #include, #fuses,
and #use statements, as well as all variable declarations. |
|
|
Guest
|
|
Posted: Fri Nov 04, 2005 7:12 pm |
|
|
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
|
|
|
Guest
|
|
Posted: Fri Nov 04, 2005 8:20 pm |
|
|
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 |
|
|
|
|
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
|