ElecDesigner
ElecDesigner

Reputation: 59

Passing String Litteral to Function that takes a pointer

If I have the function that takes a pointer to a string like this:

void outTxData(uint8_t *outString)
{
//Do something.

}

How do I pass a string literal to it? I.E. I want to just do.....

outTxData("HELLO") but I can't as the function takes a pointer. What's the easiest way to get around this?

I've tried all sorts to no avail. I dont know how to make a pointer to a string literal.

Upvotes: 0

Views: 57

Answers (1)

Solomon Ucko
Solomon Ucko

Reputation: 6109

Try casting the string literal, e.g.

void outTxData(uint8_t *outString)
{
//Do something.

}

outTxData((uint8_t*) "HELLO);

or

void outTxData(char *outStringCharPointer)
{
uint8_t* outString = (uint8_t*) outStringCharPointer;
//Do something.
}

outTxData("HELLO");

Note that char const * or uint8_t const * is best, so that you don't accidentally cause UB by trying to modify the contents of the string literal.

Upvotes: 0

Related Questions