|
-
Jun 16th, 2004, 02:59 PM
#1
Thread Starter
Hyperactive Member
char[]
I am using a low level c compiler for PIC. There are no string libs. I don't know how to handle char[]. For example, I wrote a function, PrintChar(char iChar); that prints a single char to an LCD. How can I pass a char[] and print the whole string instead of a single char without str functions? I am not quit sure how to loop through the char[]. If someone could post examples or links that would be great. thx
-
Jun 16th, 2004, 03:17 PM
#2
Code:
void PrintChar(const char* szText)
{
// as long as szText is not pointing to a null char
while ( szText != '\0' )
{
// print the current character
// and move the pointer to the next one
PrintChar( (*szText++) );
}
}
or you could pass in the length of the string:
Code:
void PrintChar(const char* szText, int length)
{
for(int i = 0; i < length; ++i)
PrintChar(szText[i]);
}
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Jun 16th, 2004, 03:30 PM
#3
Fanatic Member
How low is this C complier? If you are able to do assembler, there is a quick way to machine-gun a string to display (sorry, 80x86 assembler):
http://burks.brighton.ac.uk/burks/la...smtut/asm8.htm
You could include chunks like this if you use the asm keyword.
"Can't" and "shouldn't" are two totally separate things.
All questions should be answered. All answers should be true. That is why I post.
-
Jun 16th, 2004, 03:34 PM
#4
Thread Starter
Hyperactive Member
Hey thanks. Here is what it ended up being. The compiler is picky so I had to put ++ operator in a different place. It works though.
Code:
const char *hello="hello";
PrintStr(hello);
void PrintStr(const char *iChar)
{
uns8 x=0;
while(iChar[x]!='\0')
{
PrintChar(iChar[x++]);
}
}:w
-
Jun 16th, 2004, 03:40 PM
#5
Thread Starter
Hyperactive Member
Thx anyways Darkwraith. The compiler does allow for inline asm commands, but I wanted to stick to c. BTW, the proc is a PIC 18F877 (The asm code is quit different from 8086.)
Thanks for the quick help guys
-
Jun 16th, 2004, 03:51 PM
#6
Thread Starter
Hyperactive Member
One more question. Why is it necessary for the char[] to be const? I first tried writing it with a var, but the compiler does not allow for it, and all example I have seen also use const. It would save me some memory by using a var. I only have 8k of program memory, so every little bit helps.
-
Jun 16th, 2004, 04:35 PM
#7
Fanatic Member
Will it let allow you write it as:
char hello[] = "hello";
?
"Can't" and "shouldn't" are two totally separate things.
All questions should be answered. All answers should be true. That is why I post.
-
Jun 16th, 2004, 04:46 PM
#8
Thread Starter
Hyperactive Member
I tried:
char test[]="test";
the compiler says "array size required"
then I tried:
char test[20]="test";
the compiler says "static const is needed"
then I tried
char *test="test";
the comiler says "const * type modifier is required
It appears like it won't let me use a variable, just constants. This can't be the only solution can it?
-
Jun 16th, 2004, 04:59 PM
#9
Fanatic Member
My solution works with g++. I don't know what the compiler will allow you to use, though.
If you could give me a link to the compiler that you are using, I could take a look and see if there are any alternative syntaxes.
"Can't" and "shouldn't" are two totally separate things.
All questions should be answered. All answers should be true. That is why I post.
-
Jun 16th, 2004, 07:33 PM
#10
Thread Starter
Hyperactive Member
Here is a link to the compiler.
http://www.bknd.com/cc8e/index.shtml
The only examples I saw all used const. I am going to send him an email. He is usually good about answering questions.
-
Jun 17th, 2004, 07:32 AM
#11
Monday Morning Lunatic
Any string in double quotes in your source file will be produced as a constant when compiled. In some cases they may end up in read-only memory.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jun 17th, 2004, 02:01 PM
#12
A good reason for the above is that if in several different places in your code, you use a string literal the compiler doesn't have to place it in your executable N times; it can just place it there once and then all instances of that string literal will point to the same place in memory.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Jun 17th, 2004, 02:05 PM
#13
Thread Starter
Hyperactive Member
parksie was correct. I contacted the author. This compiler ends up placing the stings in ROM, and this compiler's pointers can only point to ROM (can't point to RAM).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|