Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22067

C string literal as parameter equals -1 in avr-gcc?

$
0
0

I am developing a software for AVR microcontroller. Saying in fromt, now I only have LEDs and pushbuttons to debug. The problem is that if I pass a string literal into the following function:

void test_char(const char *str) {  if (str[0] == -1)    LED_PORT ^= 1 << 7;         /* Test */}

Somewhere in main()

test_char("AAAAA");

And now the LED changes state. On my x86_64 machine I wrote the same function to compare (not LED, of course), but it turns out that str[0] equals to 'A'. Why is this happening?

Update:Not sure whether this is related, but I have a struct called button, like this:

typedef struct {  int8_t seq[BTN_SEQ_COUNT];    /* The sequence of button */  int8_t seq_count;             /* The number of buttons registered */  int8_t detected;              /* The detected button */  uint8_t released;             /* Whether the button is released                                   after a hold */} button;button btn = {  .seq = {-1, -1, -1},  .detected = -1,  .seq_count = 0,  .released = 0};

But it turned out that btn.seq_count start out as -1 though I defined it as 0.

Update2

For the later problem, I solved by initializing the values in a function. However, that does not explain why seq_count was set to -1 in the previous case, nor does it explain why the character in string literal equals to -1.

Update3

Back to the original problem, I added a complete mini example here, and same occurs:

void LED_on() {  PORTA = 0x00;}void LED_off() {  PORTA = 0xFF;}void port_init() {  PORTA = 0xFF;  DDRA |= 0xFF;}void test_char(const char* str) {    if (str[0] == -1) {        LED_on();    }}void main() {  port_init();  test_char("AAAAA");  while(1) {  }}

Update 4

I am trying to follow Nominal Animal's advice, but not quite successful. Here is the code I have changed:

void test_char(const char* str) {    switch(pgm_read_byte(str++)) {        case '\0': return;        case 'A': LED_on(); break;        case 'B': LED_off(); break;    }}void main() {  const char* test = "ABABA";  port_init();  test_char(test);  while(1) {  }}

I am using gcc 4.6.4,

avr-gcc -vUsing built-in specs.COLLECT_GCC=avr-gccCOLLECT_LTO_WRAPPER=/home/carl/Softwares/AVR/libexec/gcc/avr/4.6.4/lto-wrapperTarget: avrConfigured with: ../configure --prefix=/home/carl/Softwares/AVR --target=avr --enable-languages=c,c++ --disable-nls --disable-libssp --with-dwarf2Thread model: singlegcc version 4.6.4 (GCC) 

Viewing all articles
Browse latest Browse all 22067

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>