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[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 the char array str[10] works while the string pointer
str* doesn't work?
Thanks!
Frank 2 1973
On 10 Jun 2004 16:12:38 -0700, yo******@yahoo.com (Frank) wrote: 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[i] != 0; i++) { putc(str[i]); } 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>>
Frank wrote: 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[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 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;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email) http://www.geocities.com/abowers822/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Espen Ruud Schultz |
last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a
pointer to the std::string's "content" so that the char pointer can point to
the same text? And vice versa; can I give...
|
by: Marcin Kalicinski |
last post by:
Why string literals are regarded as char * not as const char *?
(1) void f(char *);
(2) void f(const char *);
f("foo") will call version (1) of function f.
I understand that the exact type...
|
by: Davy |
last post by:
Hi all,
I found
char x={"my"};
can be compiled.
But
char x;
x={"my"};
can not be compiled.
|
by: Don Starr |
last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string
(including nul), or the size of a pointer?
For example, assuming a char is 1 byte and a char *...
|
by: al |
last post by:
char s = "This string literal";
or
char *s= "This string literal";
Both define a string literal. Both suppose to be read-only and not to be
modified according to Standard. And both have...
|
by: songkv |
last post by:
Hi,
I am trying to reassign an array of char to a string literal by
calling a function.
In the function I use pointer-to-pointer since I want to reassign the
"string array pointer" to the string...
|
by: Simon Schaap |
last post by:
Hello,
I have encountered a strange problem and I hope you can help me to
understand it. What I want to do is to pass an array of chars to a
function that will split it up (on every location where...
|
by: Nascimento |
last post by:
Hello,
How to I do to return a string as a result of a function.
I wrote the following function:
char prt_tralha(int num)
{
int i;
char tralha;
|
by: Andrea |
last post by:
Hi,
suppose that I have a string that is an hexadecimal number, in order
to print this string I have to do:
void print_hex(unsigned char *bs, unsigned int n){
int i;
for (i=0;i<n;i++){...
|
by: Sumit |
last post by:
hi,
I have a url which is a char *
char * url = "/url=%2Fhome%2Fsumit%2Fpackages%2Fmyxml.xml";
when i send this URL to http server it was like -
"/url=/home/sumit/pacakges/myxml.xml"
but...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |