pinoxxio
pinoxxio

Reputation: 38

Put Integer Pointer into Array

I tried to put the Ptr at the end of the frame in C.

void myFunction(uint8_t * Ptr)
{
 uint8_t frame[] = {0x1, 0x2, 0x3, *Ptr}
}

int _main()
{
 myFunction("Hello");
}

in this solution I put only the H of "Hello" into frame[4] because of the 8 bit.

After that I tried

    strcat((char*)frame, (char*) StateTxtPtr);

but it didn't work.

The solution should look like this: frame = {0x1, 0x2, 0x3, "H", "E", "L", "L", "O"}

Thanks for your help!

SOLUTION **

void myFunction(uint8_t * Ptr, uint32_t TxtSize)
{
 uint8_t frame[25] = {0x1, 0x2, 0x3, *Ptr}
 memcpy(&frame[3], Ptr, TxtSize); 
}

int _main()
{
 uint32_t TxtSize = strlen((char *)&txt[i][0]);
 myFunction("Hello", TxtSize);
}

Upvotes: 0

Views: 118

Answers (1)

unwind
unwind

Reputation: 400159

Your question doesn't make any sense.

You probably want something like this:

uint8_t frame[8] = { 1, 2, 3 };

memcpy(frame + 3, "Hello", 5);

Note that frame must have room for the characters, and that memcpy() is being used to avoid writing the '\0'-terminator that strings have.

Upvotes: 6

Related Questions