473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Repost: String pointer and char array

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
Nov 14 '05 #1
7 1865
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

Nov 14 '05 #2
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
Nov 14 '05 #3
"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.
Nov 14 '05 #4
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
Nov 14 '05 #5


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.

Nov 14 '05 #6
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.

[....]

Nov 14 '05 #7
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;
}
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
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...
28
by: Davy | last post by:
Hi all, I found char x={"my"}; can be compiled. But char x; x={"my"}; can not be compiled.
16
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 *...
7
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...
4
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...
4
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...
23
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;
6
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++){...
17
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.