Results 1 to 13 of 13

Thread: char[]

  1. #1

    Thread Starter
    Hyperactive Member Sneeden's Avatar
    Join Date
    Oct 2001
    Location
    Sneedville
    Posts
    258

    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

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    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.

  3. #3
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    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.

  4. #4

    Thread Starter
    Hyperactive Member Sneeden's Avatar
    Join Date
    Oct 2001
    Location
    Sneedville
    Posts
    258
    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

  5. #5

    Thread Starter
    Hyperactive Member Sneeden's Avatar
    Join Date
    Oct 2001
    Location
    Sneedville
    Posts
    258
    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

  6. #6

    Thread Starter
    Hyperactive Member Sneeden's Avatar
    Join Date
    Oct 2001
    Location
    Sneedville
    Posts
    258
    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.

  7. #7
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    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.

  8. #8

    Thread Starter
    Hyperactive Member Sneeden's Avatar
    Join Date
    Oct 2001
    Location
    Sneedville
    Posts
    258
    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?

  9. #9
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    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.

  10. #10

    Thread Starter
    Hyperactive Member Sneeden's Avatar
    Join Date
    Oct 2001
    Location
    Sneedville
    Posts
    258
    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.

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  12. #12
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    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.

  13. #13

    Thread Starter
    Hyperactive Member Sneeden's Avatar
    Join Date
    Oct 2001
    Location
    Sneedville
    Posts
    258
    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
  •  



Click Here to Expand Forum to Full Width