Re: What's wrong with the string?
From: Bill Pringlemeir (spam_account_at_sympatico.ca)
Date: 06/21/04
- Previous message: Rushi: "PPPD and MGETTY"
- In reply to: Freddy: "Re: What's wrong with the string?"
- Next in thread: Lew Pitcher: "Re: What's wrong with the string?"
- Reply: Lew Pitcher: "Re: What's wrong with the string?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 21 Jun 2004 16:08:51 -0400
On Sun, 20 Jun 2004, blahhblaah@hotmail.com wrote:
> Hi Frank,
>
>> void example() {
>> char *str = "this is a test";
>
> Not an answer to your question, but I would suggest you change the
> latter line to:
> char str[] = "this is a test";
No, these are pretty much equivalent. The original is correct here.
It might be something to do with memory maps if the code is in some
startup code. Then it is better to use "const char *" so that the
string is places with code. There might be no DRAM mapped... but the
OP didn't seem to indicate anything like this.
>> void example() {
>> char *str = "this is a test";
>> while (*str != 0) {
>> putc(*str++);
>> }
>> return;
>> }
>>
>> putc() is a function to put a char on the serial console.
>> i think putc is a macro and not a function
I think this is key. putc will evaluate "*str++". If it is evaluated
multiple times, the str could point beyond the end of your string. In
the array version, the increment within the parameters to putc are
avoided. I would try this version,
void example() {
char *str = "this is a test";
while (*str != 0) {
putc(*str);
str++;
}
return;
}
"while(*str != '\0')" is technically more correct as you are comparing
characters to characters. However, C's promotion rules will end up
converting everything to ints anyway, so it will effectively be the
same. Ie, I doubt that is the problem.
fwiw,
Bill Pringlemeir.
-- I work hard because millions on welfare depend on me.
- Previous message: Rushi: "PPPD and MGETTY"
- In reply to: Freddy: "Re: What's wrong with the string?"
- Next in thread: Lew Pitcher: "Re: What's wrong with the string?"
- Reply: Lew Pitcher: "Re: What's wrong with the string?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|