Repost: String pointer and char array
From: Frank (young726_at_yahoo.com)
Date: 06/11/04
- Next message: Bill Zhao: "Re: What's wrong with the string?"
- Previous message: Frank: "What's wrong with the string?"
- Next in thread: tweak: "Re: Repost: String pointer and char array"
- Reply: tweak: "Re: Repost: String pointer and char array"
- Reply: Jack Klein: "Re: Repost: String pointer and char array"
- Reply: nospam_at_nospam.org: "Re: Repost: String pointer and char array"
- Reply: Frank: "Re: Repost: String pointer and char array"
- Reply: Robert Stankowic: "Re: Repost: String pointer and char array"
- Reply: RoSsIaCrIiLoIA: "Re: Repost: String pointer and char array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 10 Jun 2004 17:24:08 -0700
Sorry that the question I posted few minutes ago didn't correctly
describe the problem. So please ignore it. I repost the question as
below:
==============================================
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
"><...HH..>>..."
Same things happens if I use
char str[10] = \
{'t','h','i','s',' ','i','s',' ','a',' ','t','e','s','t','\0'};
to assign str.
But if I modify the function to the following one:
void example_modified() {
char str[10];
str[0] = 't';
str[1] = 'e';
str[2] = 's';
str[4] = 't';
str[5] = '\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 assigning string str individually works while assigning
them together doesn't work? Isn't str[]="test" a shortcut of
" str[0] = 't';
str[1] = 'e';
str[2] = 's';
str[4] = 't';
str[5] = '\0';"?
Thanks!
Frank
- Next message: Bill Zhao: "Re: What's wrong with the string?"
- Previous message: Frank: "What's wrong with the string?"
- Next in thread: tweak: "Re: Repost: String pointer and char array"
- Reply: tweak: "Re: Repost: String pointer and char array"
- Reply: Jack Klein: "Re: Repost: String pointer and char array"
- Reply: nospam_at_nospam.org: "Re: Repost: String pointer and char array"
- Reply: Frank: "Re: Repost: String pointer and char array"
- Reply: Robert Stankowic: "Re: Repost: String pointer and char array"
- Reply: RoSsIaCrIiLoIA: "Re: Repost: String pointer and char array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|