ElecDesigner
ElecDesigner

Reputation: 59

STM32 (C) Trouble passing string literals and variables to a function in one go

I have a function that takes a pointer to a some data i.e.

void TxData(uint8_t *outString)
{
     // Do something.
}

Currently I am passing a mix of string literals and variables (all ascii data) to the function. E.G.

TxData((uint8_t *) "Setting");
TxData((uint8_t *) Info1);
TxData((uint8_t *) "=");
TxData((uint8_t *) Info2);
TxData((uint8_t *) "\r");

This all works but I wonder if I can do this in one line of code (i.e. something like concatenate so I only call TxData once). I don't want to do anything that will impact performance as this is a fairly low end Microcontroller (so don't want the strings to be copied) but want to tidy the code.

Upvotes: 1

Views: 601

Answers (1)

0___________
0___________

Reputation: 68034

low end Microcontroller (so don't want the strings to be copied)

Then keep it as it is. You can also write the function which will take an array of char *.

const char * const toprint[] = {"Setting" ,Info1, "=", Info2, "\r", NULL};

void myTcData(const char * const ptr[])
{
    while(*ptr)
    { 
          TxData(*ptr);
          ptr++;
    }
}

It will not waste any precious RAM memory and the table will be kept in the FLASH.

I would also change the prototype of the TxData

void TxData(const void *ptr)
{
    const uint8_t *outstring = ptr;
    /* .... */
}

Upvotes: 3

Related Questions