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 7 1773
It looks like your want to create an array and print it out.
So we can have:
#include <stdio.h>
int
main(void) {
/* declare and assign character array */
char sA[40] = "this is a test";
/* declare character pointer */
char *pA;
/* assign character array (char sA[0]) to pointer */
pA = sA;
/* print pointer array */
puts(pA);
return 0;
}
Is the above what you are trying to do? If you want to
do loop and print out each character at a time, just
use pointer math.
I think you need to study pointers more. Pointers
contain memory addresses, not values.
Brian
Frank wrote: 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
On 10 Jun 2004 17:24:08 -0700, yo******@yahoo.com (Frank) wrote in
comp.lang.c: 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.
As somebody already pointed out, putc() is a standard C library
function that requires two arguments. If your compiler provides a
function by that name that takes only one argument, it is severely
broken from a C language point of view. To the point where whatever
happens is not a language issue, and is off-topic in comp.lang.c.
Your question may or may not be topical in the embedded Linux group, I
don't know. But leave out the cross-posts to comp.lang.c, please, it
doesn't belong here.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
"Jack Klein" <ja*******@spamcop.net> wrote: "Frank" wrote: 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. As somebody already pointed out, putc() is a standard C library function that requires two arguments. If your compiler provides a function by that name that takes only one argument, it is severely broken from a C language point of view.
Not if it is a standalone implementation (non-hosted). There are no
requirements to provide the <stdio.h> capability of the C library. In fact,
none of the library functions are required, only macro definitions such as
<limits.h>. I see no reason why such an implmentation couldn't reuse the
putc identifier to provide some similar facility within the capabilities of
the particular embedded system.
To the point where whatever happens is not a language issue, and is off-topic in comp.lang.c.
It's true, we don't discuss standalone implementations in comp.lang.c; I
hear comp.arch.embedded is a good place.
--
Simon.
In <cb******************************@news.teranews.co m> "Ralmin" <ne**@ralminNOSPAM.cc> writes: "Jack Klein" <ja*******@spamcop.net> wrote: "Frank" wrote: > 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.
As somebody already pointed out, putc() is a standard C library function that requires two arguments. If your compiler provides a function by that name that takes only one argument, it is severely broken from a C language point of view.
Not if it is a standalone implementation (non-hosted). There are no requirements to provide the <stdio.h> capability of the C library. In fact, none of the library functions are required, only macro definitions such as <limits.h>. I see no reason why such an implmentation couldn't reuse the putc identifier to provide some similar facility within the capabilities of the particular embedded system.
1. Because the standard library identifier putchar would be better suited
for this purpose.
2. If a freestanding library chooses to use identifiers from the standard
C library, it better preserves their semantics. It's not required by
the standard but it's an elementary quality of implementation issue;
would you use such a library where the semantics of putchar(x) were:
read a byte from I/O port x?
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Frank wrote: 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
It could also be a serial comms handshaking problem. The second string is
shorted so doesn't cause the receiver beffer to overflow and drop bits.
Just a thought.
Frank wrote:
Ok, Frank, what is the major difference between the failing and the
working code? 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:
Try these changes:
void example() { char *str = "this is a test";
int i;
for(i = 0; str[i]; ++i)
{
putc(str[i]);
}
/* while (*str != 0) { putc(*str++); }*/ return; }
If that works, then something with the precedence in *str++ is wrong,
I'd guess.
[....]
On 10 Jun 2004 17:24:08 -0700, yo******@yahoo.com (Frank) wrote: 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; }
void example()
{
char *str = "this is a test";
while (*str != 0) {putc(*str++);}
fflush(stdout); /* note fflush(stdout); */
return;
} This discussion thread is closed Replies have been disabled for this discussion. Similar topics
10 posts
views
Thread by Marcin Kalicinski |
last post: by
|
28 posts
views
Thread by Davy |
last post: by
|
16 posts
views
Thread by Don Starr |
last post: by
|
7 posts
views
Thread by al |
last post: by
|
4 posts
views
Thread by songkv |
last post: by
|
4 posts
views
Thread by Simon Schaap |
last post: by
|
23 posts
views
Thread by Nascimento |
last post: by
|
6 posts
views
Thread by Andrea |
last post: by
|
17 posts
views
Thread by Sumit |
last post: by
| | | | | | | | | | |