[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] Bizarre string problem, part 3
From: |
Eric Fu |
Subject: |
Re: [avr-gcc-list] Bizarre string problem, part 3 |
Date: |
Thu, 9 Sep 2004 12:33:25 +1000 |
Andre Wrote:
----- Original Message -----
From: "André - BOL" <address@hidden>
To: <address@hidden>
Sent: Thursday, September 09, 2004 10:30 AM
Subject: [avr-gcc-list] Bizarre string problem, part 3
> Hi!
>
> First, I'd like to thank everybody for the tips, but they haven't
> worked. I formulated a new very simple example to demonstrate the
> problem I'm facing.
>
> Suppose I "initialize" a variable in some different and equivalent ways:
> char a[10] = {'T', 'T', 'T', 'T', 'T' };
> char b[] = "TTTTTTT";
> char c[10]; c[0]='T'; c[1]='T'; c[2]='T'; c[3]='T';
> //i think this can't be called an initialization!!!
> char *d = "TTTTTTT"
>
> now, I perform the test
>
> if ( x[2] == 'T' ) printf("hi"); // x = a, b, c or d
>
> All examples are LEGAL STANDARD PURE C. I chose size 10 and the 3rd
> ( [2] ! ) element to avoid avoid any doubts that I had enougth space
> reserved, and that I took the correct character. All tests should return
> true and print "hi". My example ttest.c is to be run on Pc and shows
> exactly that with printf's
>
> PROBLEM: when using the very same code on the ATMEGA, changing the
> printf's for "turn on led 1", "turn on led 2", etc., only the third char
> sequence (C) causes correct evaluation of the if expression (i can see
> that, because I have the leds correctly connected and only the 3rd one
> lights)
>
> I tryed this under WINDOWS AND UNDER LINUX, using avr-gcc.
>
> Compilation and downloading commands:
>
> avr-gcc -g -mmcu=atmega32 -Wall -o t.o t.c
> avr-objcopy -j .text -O ihex t.o t.hex
> uisp -dprog=dapa --erase
> uisp -dprog=dapa --upload if=t.hex -dno-poll -v=3 --hash=32
>
>
I think the problem is that there is a diffference between " T " and ' T ',
where " T " = ' T ' + ' \0 '.
So
char a[10] = {'T', 'T', 'T', 'T', 'T' }; is equal to:
char a[10] = {' T ', ' \0 ', ' T ', ' \0 ',' T ', ' \0 ',' T ', ' \0
',' T ', ' \0 ',}; Then a[2] = ' T '.
Likewise, c[2]='T'; is equal to c[2]= ' T '; c[2]= ' \0 '; So c[2] end s up
to be ' \0 ' instead of expected ' T '.
Cheers,
Eric Fu
- [avr-gcc-list] Bizarre string problem, part 3, André - BOL, 2004/09/08
- Re: [avr-gcc-list] Bizarre string problem, part 3,
Eric Fu <=
- Re: [avr-gcc-list] Bizarre string problem, part 3, Ian Caddy, 2004/09/08
- Re: [avr-gcc-list] Bizarre string problem, part 3, Richard Urwin, 2004/09/09
- Re: [avr-gcc-list] Bizarre string problem, part 3, Ian Caddy, 2004/09/09
- Re: [avr-gcc-list] Bizarre string problem, part 3, Joerg Wunsch, 2004/09/09