Repost: String pointer and char array

F

Frank

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 != 0; i++) {
putc(str);
}
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
 
T

tweak

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
 
J

Jack Klein

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.
 
R

Ralmin

Jack Klein said:
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.
 
D

Dan Pop

In said:
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
 
G

Guest

Frank said:
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 != 0; i++) {
putc(str);
}
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.
 
R

Robert Stankowic

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)
{
putc(str);
}
/*
while (*str != 0) {
putc(*str++);
}*/
return;
}

If that works, then something with the precedence in *str++ is wrong,
I'd guess.

[....]
 
R

RoSsIaCrIiLoIA

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,800
Messages
2,569,657
Members
45,417
Latest member
BonitaNile
Top