String pointer and char array

F

Frank

Hi,

I am developing an application on PPC405 (Walnut). The compiler I am
using is GCC. But somehow the string pointer in a 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..>>..."

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 != 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 the char array str[10] works while the string pointer
str* doesn't work?

Thanks!

Frank
 
B

Barry Schwarz

Hi,

I am developing an application on PPC405 (Walnut). The compiler I am
using is GCC. But somehow the string pointer in a 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.

putc is a standard function which requires two arguments.
This function ends up with a strange string on the serial console
"><...HH..>>..."

After changing the function name to main and fixing the syntax error
in the call to putc, it produced the expected output. I have no idea
why it doesn't on your system. Maybe you should post all the code
since it is likely that something else is causing the problem.
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 != 0; i++) {
putc(str);
}
return;
}

Then it works. So I am wondering if there is something wrong with the


This cannot work, even though it may appear to. Since str has room
for only 10 char, it is initialized to 't', 'h', 'i', 's', ' ', 'i',
's', ' ', 'a', ' '. Notice that there is no '\0' in the array. The
terminating condition in your for loop will not be reached until after
i has exceeded the size of the array. This invokes undefined
behavior.
stack setting. Can anybody tell me what makes the above two functions
different? Why the char array str[10] works while the string pointer
str* doesn't work?

You need to provide a complete, correct program that demonstrates the
behavior you are claiming.



<<Remove the del for email>>
 
A

Al Bowers

Frank said:
Hi,

I am developing an application on PPC405 (Walnut). The compiler I am
using is GCC. But somehow the string pointer in a 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..>>..."

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 != 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 the char array str[10] works while the string pointer
str* doesn't work?

Both functions are congruous in flaw. I would first suggest you get
the code correct rather than worry about something wrong with the
stack setting, or , why you got different results.

Include header stdio.h. You will notice that the function putc is
wrong. Change it to function putchar. Add a newline char to the end
of the string and then it should work as expected.

#include <stdio.h>

void example(void)
{
char *str = "this is a test\n";
while (*str != 0)
{
putchar(*str++);
}
return;
}

int main(void)
{
example();
return 0;
}
 

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
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top