-
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
-
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]);
}
-
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.
-
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
-
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 :)
-
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.
-
Will it let allow you write it as:
char hello[] = "hello";
?
-
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?
-
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.
-
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.
-
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.
-
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.
-
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).