Re: What's wrong with the string?
From: Freddy (blahhblaah_at_hotmail.com)
Date: 06/20/04
- Next message: Freddy: "Re: DEVICE DRIVER DEVELOPMENT IN EMBEDDED LINUX SYSTEMS"
- Previous message: Earthlink: "read-only Filesystem on boot?"
- In reply to: Frank: "What's wrong with the string?"
- Next in thread: Bill Pringlemeir: "Re: What's wrong with the string?"
- Reply: Bill Pringlemeir: "Re: What's wrong with the string?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 20 Jun 2004 20:45:27 +0200
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";
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'.
> while (*str != 0) {
> putc(*str++);
putc is used to put a character from a stream to the screen (see man
putc). You might want to use:
putchar(*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
> "><...HH..>>..."
>
> 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'};
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'};
> 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?
See above.
Good luck!
Freddy
- Next message: Freddy: "Re: DEVICE DRIVER DEVELOPMENT IN EMBEDDED LINUX SYSTEMS"
- Previous message: Earthlink: "read-only Filesystem on boot?"
- In reply to: Frank: "What's wrong with the string?"
- Next in thread: Bill Pringlemeir: "Re: What's wrong with the string?"
- Reply: Bill Pringlemeir: "Re: What's wrong with the string?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|