Jump to content

Char array


Rio
 Share

Recommended Posts

hi,

i start programming on C, and have 3 little questions to it.

1. i've declare a :

char text[41];
fill it up with chars and want to put out on LCD:
MIOS_LCD_PrintCString(text);
why isn't that able? something is written in compiler error about invalid types. i see that there is a "code" before char* str in PrintCString declaration. What is the meaning of that? special type? Can't i send a complete char array to LCD... only const strings or is there a other way? 2...so i've try to put out single chars and store in my char array: first i get in letters and store them in array...
MIOS_LCD_CursorSet(MIOS_LCD_CursorGet());

text[MIOS_LCD_CursorGet()] = letter;   // letter is a char
text[MIOS_LCD_CursorGet()+1] = '\0';
MIOS_LCD_PrintChar(text[MIOS_LCD_CursorGet()]);
MIOS_LCD_CursorSet(MIOS_LCD_CursorGet()+1);
..end here later, i want to output the whole char array on LCD:
unsigned char i;

MIOS_LCD_CursorSet(0x00);

for(i=0; i<41; i++) {
  if (text[i] == '\0') break;
  MIOS_LCD_PrintChar(text[i]);

}

..but if input a "HELLO" then i get back a "HELLO+filled Rectangle char". Don't understand... seems there is something wrong in my input routine (MIOS_LCD_CursorGet / MIOS_LCD_CursorSet) ?

the LCD shows me not the 5 letters HELLO, but instead the first 6 array entrys (HELLO + a Rectangle) and then after Rectangle it breaks...

Do someone knows a hint?

3. if i want use strlen() Funktion i got compiling error:

..

// somewhere in code:

    strlen(test);

..

Linking project

error: missing definition for symbol "_strlen", required by "_output\main.o"

ERROR!

Process terminated with status 1 (0 minutes, 3 seconds)

0 errors, 0 warnings

i've additional included in main.c:

#include <string.h>

I don't understand? Any helps?

Link to comment
Share on other sites

Hi Rio,

something is written in compiler error about invalid types.

in 95% of all requests it's always the same:

- "there is an error"

- "it won't work"

- "the compiler outputs an error message"

Why don't you post this error message? ::)

It gives us all the information necessary to get it right!

Sorry for the Anstinker, now resuming in good mood ;D ;) :

-> have you checked if your array is one byte longer than all chars occuring in this word? I guess so, because you use 41 chars, but it would explain the troubles. Btw: you don't have to care about the terminated null-char at the end of the array (exept when setting the array length).

Perhaps the "invalid type" is your null-termination '\0' ? You may not add that to your array. Just leave it and it's fine (see examples below).

char text[41];

first: you better want to use unsigned chars, that's a bit more economic than a (signed) char.

It's not clear to me, what you are trying to do with this char array.

I guess, the problem is that you are trying to print out words but refer to chars; it seems a bit mixed up.

If you want to store the word "HELLO" it's:

[tt]const unsigned char myWord[6] = { "HELLO" } [/tt]

but it makes much more sense to use word arrays where you can easily select the word:

[tt]const unsigned char myWords[3][6] = {

"HELLO",

"BYE..",

"OKAY#"

}

[/tt]

I have implemented this and printed the words like this:

[tt]MIOS_LCD_PrintCString( (myWords[n]) ); [/tt]

It works for me, so I don't know else what to say without knowing the exact errormessage.

to 3:

error: missing definition for symbol "_strlen", required by "_output\main.o"

if you want to use strlen() and it's not included in the basic SDCC library, you will have to implement that by yourself. "Missing definition" explains this pretty well.

It's like you wanted to use srand() or sin(); all of these well-known functions have to be included before you can use them.

Cheers ;)

Michael

Link to comment
Share on other sites

for 1:

in 95% of all requests it's always the same:

- "there is an error"

- "it won't work"

- "the compiler outputs an error message"

Why don't you post this error message?  Roll Eyes

It gives us all the information necessary to get it right!

the compiler tells me that i'm using with MIOS_LCD_PrintCString(text);

an invalid type, nothing more.

-> have you checked if your array is one byte longer than all chars occuring in this word? I guess so, because you use 41 chars, but it would explain the troubles. Btw: you don't have to care about the terminated null-char at the end of the array (exept when setting the array length).

Perhaps the "invalid type" is your null-termination '\0' ? You may not add that to your array. Just leave it and it's fine (see examples below).

i know these consideration of char arrays (working every day with that).. but didn't get working with that MIOS_LCD_PrintCString function :-\!!

I found in declaration: void MIOS_LCD_PrintCString(code char* str);

It seems that this code in front of char* is the reason for that ???? is code a own typedef or what; i've never seen such before. And does accept this "code char*" only const char arrays?

For my Project i need a variable char array!! not const... ;)

first: you better want to use unsigned chars, that's a bit more economic than a (signed) char.
Are you sure?? and why?

for 2. Does anyone knows a reason for that behavior with MIOS_LCD_CursorGet / MIOS_LCD_CursorSet in my code... i think the \0 will be stored in a higher position as it should be in LCD... mhh...

thxs for 3. what i've to do to implement that by myself ???

Link to comment
Share on other sites

void MIOS_LCD_PrintCString(code char* str); seems that this code in front of char* is the reason for that? is code a own typedef or what;

I don't know what "code" is? When you don't know it, where does it come from?

Just use [tt]unsigned char str[/tt] like I already written in my example above.

If you're using pointers, you probably have to include the SDCC lib.

For my Project i need a variable char array!! not const...

Then leave away the const keyword and it's no constant anymore.

Btw: here's a quite nice openBook for C in german language:

http://www.galileocomputing.de/openbook/c_von_a_bis_z/c_007_007.htm#RxxobKap0070070400263C1F01E18C

Quote

first: you better want to use unsigned chars, that's a bit more economic than a (signed) char.

Are you sure?? and why?

Because a char used as alphabetic letter is never negative. And my C-Book sais:

char: 1 Byte, -128 to 127 or 0 to 255*

signed char: 1 Byte, -128 to 127

unsigned char: 1 Byte, 0 to 255

* that 'or' is the problem. Do you know how SDCC or the PIC interprets this byte?

I guess that SDCC will translate [tt]char[/tt] as a [tt]unsigned char[/tt]. But as you'll never know how SDCC might change it's behaviour in future or you might port your code to some other platform where it'll get interpreted as [tt]signed char[/tt], you will get problems if you're unclear. So try to be clear and use [tt]unsigned[/tt] whenever possible.

for 3. what i've to do to implement that by myself

believe me, it's better to deal with strings that have the same length. Why do you need a stringlength counter? The only reason I can think of are patch names. But even those are better defined with a certain length and prefilled with emtpy spaces.

Regards,

Michael

Link to comment
Share on other sites

1. na also ich habs grad getestet mit const unsigned char x[41] (welche im main header deklariert wäre) funktioniert die PrintCString Fkt.

Mit "nicht konstanten" char arrays (oder halt auch unsigned char arrays) (welche nur im C-file deklariert werden dürfen) kommt folgende fehlermeldung:

-------------- Build: MBHP in MB-KB ---------------
Compiling: main.c
Makefile generated.
Makefile.bat generated.
Assembling MIOS SDCC wrapper
==========================================================================
Compiling pic18f452.c
Processor: 18F452
sdcc: Calling preprocessor...
sdcc: Generating code...
==========================================================================
Compiling sm_simple.asm
==========================================================================
Compiling main.c
Processor: 18F452
sdcc: Calling preprocessor...
main.c:274: error 78: incompatible types
sdcc: Generating code...
from type 'char [41] '
ERROR!
to type 'char code* '
main.c:313: error 103: code not generated for 'SM_NotifyToggle' due to previous errors
Process terminated with status 1 (0 minutes, 3 seconds)
2 errors, 0 warnings

als bei unsigned das selbe.....ich hab keine erklärung bisher dafür.

und 2. hat sich geklärt...meine undurchsichtigkeit, hing mit anderem code zusammen ;D

Grüße Rio.

Link to comment
Share on other sites

Hi Rio,

the code keyword indicates that the pointer points to a code memory location

if you look at the source below you can see that the table pointer is used

this is the reason why no  arrays (in data memory) can be used with this function...

the functionality of MIOS_PrintCString depends on this code:

.MIOS_LCD_PrintCString code
_MIOS_LCD_PrintCString
	global	_MIOS_LCD_PrintCString

	movwf	TBLPTRL
	movff	FSR0L, FSR2L
	movff	PREINC2, TBLPTRH
	movff	PREINC2, TBLPTRU
_MIOS_LCD_PrintCStringLoop
	tblrd*+
	movf	TABLAT, W
	bz	_MIOS_LCD_PrintCString_End
	call	MIOS_LCD_PrintChar
	bra	_MIOS_LCD_PrintCStringLoop
_MIOS_LCD_PrintCString_End
	return

	END
Michaël edit: here's the sourcecode from strlen:
int strlen ( char * str ) 
{
	register int i = 0 ;

	while (*str++) 
		i++ ;

	return i;
}
and the definition:
extern int   strlen (char *  )  ;

note that you've got to include the sdcc lib in your linker file

it seems that TK selected the files included in the mios lib

if you look in your sdcc directory under src you can see all of the other files

I think it's possible to include other sources

but they would have to be modified by the fixasm script to work with MIOS

Link to comment
Share on other sites

  • 1 month later...

Hello Rio,

did you find a solution to your problem? I'm just facing the same, I need to print out a non-constant string. The only way I can make it to work is repeatedly call MIOS_PrintChar, but that just can't be the solution!

So question:

How do I print a non-constant string? Where does this have to be declared to work?

Many thanks

Reiner

Link to comment
Share on other sites

IIRC "MIOS_LCD_PrintCString" is not supported by MIOS itself, but by the wrapper. That means, it makes no difference in what way you call it, because it'll be printed out char by char anyway.

Maybe codesize does increase, but I think you want to use this to print out SysEx-Messages; so these are coming in char-by-char anyway, right?

Cheers,

Michael

Link to comment
Share on other sites

Hello Michael,

thanks for the reply!

Guess if PrintCString calls PrintChar repeatedly anyway, it won't make a difference.

The problem I am having is that (due to a bug in Hauptwerk, which is the application I am interfacing with) I need to wait until I received the complete string and then fix it before printing. In Hauptwerk the two lines are not correctly written, so that the second line starts somewhere in the first line (when there is too little text in the first line) and then wraps around into the second line. At the end the whole thing is padded with blanks. Unfortunately I have no indication where the second line actually begins before I have received the complete message (I can then count the blanks from the end and shift the chars accordingly).

I have found however that this procedure is too slow and was hoping I could speed it up by printing the strings directly.

Guess I'll have to think of something else until the bug in Hauptwerk ges fixed or just live with the poorly formated output.

Thanks

Reiner

Link to comment
Share on other sites

If it's a type mismatch bug, it could be because you are passing it a variable of type char[] and it wants char*.  They are identical except some compilers will complain about it.  Just cast your array to a pointer like

MIOS_LCD_PrintCString((char *) text);

Most likely the 'code' keyword is just a hint to the compiler to say where the memory for the char* is coming from.  I don't know the details and what limitations it enforces however, but try just typecasting your array as a char pointer first.

Link to comment
Share on other sites

Another way of getting rid of such rigid compiler warnings is to use:

MIOS_LCD_PrintCString(&text[0]);

I believe this method is also pretty good for portability of the code. I've really had to use this method on the TIc55 DSP otherwise my program was not behaving as expected.

Anyway, I more have the impression that the problem stands with the non constant string and the CODE keyword in this case...

Best regards,

Lall

Link to comment
Share on other sites

Anyway, I more have the impression that the problem stands with the non constant string

..accurate!

@rainer:

The only way, I can make it to work is repeatedly call MIOS_PrintChar, but that just can't be the solution!

yes i use only "for loop" for that with PrintChar... seems that is fast enough.. i don't know another solution..

Link to comment
Share on other sites

  • 9 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...