What's wrong with the string?

Hi,

I am developing an application on PPC405 (Walnut). But somehow in the string in the C function doesn't work. The code is as below:

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.

This function ends up with a strange string on the serial console ">>..."

But if I modify the function to the following one:

void example_modified() { char str[10] = \ {'t','h','i','s',' ','i','s',' ','a',' ','t','e','s','t','\0'}; int i = 0; for (i = 0; str[i] != 0; i++) { putc(str[i]); } return; }

Then it works. So I am wondering if there is something wrong with the stack setting. Can anybody tell me what makes the above two functions different? Why using char array str[10] works while using pointer str* doesn't work?

Thanks!

Frank

Reply to
Frank
Loading thread data ...

This line should be: while(*str != '\0')

Reply to
Bill Zhao

i think putc is a macro and not a function

Reply to
rcobo

No. '\0' and 0 are equivalent in this context.

[....]
Reply to
Robert Stankowic

Hi Frank,

Not an answer to your question, but I would suggest you change the latter line to: char str[] = "this is a test";

The difference is that this option actually claims 15 bytes of memory for you. Your option just claims memory for a pointer, and the location it points to (probably NULL) will be filled with 'this is a test'.

putc is used to put a character from a stream to the screen (see man putc). You might want to use: putchar(*str++);

I would recoomend to leave the 10 out of the declaration. This will be filled in correctly by your compiler if you leave it out. In your case you fill it with 15 characters, which means you are writing memory that isn't yours! By the way, doing: char str[]="this is a test"; works as well as: char str[]={'t','h','i','s',' ','i','s',' ','a', ' ','t','e','s','t','\0'};

See above.

Good luck! Freddy

Reply to
Freddy

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.

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.
Reply to
Bill Pringlemeir

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.