Products > Programming

text / strings in C

(1/21) > >>

Simon:
I am looking to send data from a micro-controller to an LCD over SPI. Looking for discussions online yields nothing about this generically other than issues with getting the protocol to work at all.

What i am wondering is the mechanics of doing the code to accomplish this. I read that strings are not actually a thing in C. So does this mean that I create all "text" as arrays of characters and either tell my transmission function how many characters there are to cycle through or put some sort of end code in like the NUL that would be on a string and when i detect that character stop transmission.

I expect that if i were to use a string i would then have to use a pointer to move through the string so in effect may as well use an array as i can "address" each character more easily.

I am using GCC for ARM

rhodges:
I think it is easiest to terminate the characters with a zero (NUL) byte. When you use a string like "hello", the compiler will add the zero at the end for you.

Here is a snippet from my LCD library:
void lcd_puts(char *s)
{
    while (1) {
        if (*s == 0)
            return;
        lcd_putc(*s);
        s++;
    }
}

And you could do something like this:
{
    lcd_puts("Hello, world\r\n");
}
 

greenpossum:

--- Quote from: Simon on March 14, 2020, 06:58:03 pm ---I read that strings are not actually a thing in C. So does this mean that I create all "text" as arrays of characters and either tell my transmission function how many characters there are to cycle through or put some sort of end code in like the NUL that would be on a string and when i detect that character stop transmission.

--- End quote ---

I don't know what you think a string needs to be to be a thing in C. Certainly all the literature talks about strings and string functions.

When you write "hello" in a C program, the compiler treats this as an array with the characters h e l l o plus a NUL byte. All the string functions expect this NUL byte. Therefore if you write a function to output to a LCD you can stop on that NUL byte.

Here are some initialisation idioms and what they will create:

char string[] = "hello";  // string is a 6 char array with NUL as the last byte
char *string = "hello"; // string is a pointer to an anonymous 6 char array with NUL as the last byte
char string[6] = "hello";  // same as 1st case
char string[5] = "hello";  // legal, the NUL byte is omitted (this feature added to classic C for usage that didn't want to waste the last byte)

printf("Hello world\n"); // the function printf is handed the address of an anonymous 13 char array containing hello SP world NL NUL

Jeroen3:

--- Code: (dirty example) ---typedef struct {
    size_t len;
    char *string;
} string;

int string_create(string &this){
    this.len = 0;
    this.string = NULL;
}

int string_set(string &this, const char *s){
    if(this.string){
       this.len = strlen(s);
       void *t = malloc(this.len);
       strcpy(t, s);
       free(this.string);
       this.string = t;
   }else{
       this.len = strlen(s);
       this.string = malloc(this.len);
       strcpy(this.string, s);
   }
}

--- End code ---
Now strings are a thing in C!
You get the idea. C doesn't have data containers. C only has pointers to data. The rest is up to your imagination.
As programmers without containers we have settled on a standard that representations of printable text in memory are terminated with a NUL,\0,0x00 character. So this is what the compiler does.
Your protocol may utilize more of the ascii set though. If you're transmitting printable text you may as well use those.

You may be able to use a string library if that makes you feel more comfortable:
https://github.com/kozross/awesome-c#string-manipulation


--- Quote ---I expect that if i were to use a string i would then have to use a pointer to move through the string so in effect may as well use an array as i can "address" each character more easily.
--- End quote ---
[a] just means (+ (a * sizeof(type)) on a pointer.

Simon:
so long as throwing a string at something is fine for the compiler it's fine by me. So i can use a string and then need to use pointers to address the bytes or if I use

char string[] = "hello";  // string is a 6 char array with NUL as the last byte

I can just index through until i find a null character

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod