0

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);
}
4
  • And why do you use {0x1, 0x2, 0x3, *Ptr} instead of {0x1, 0x2, 0x3, Ptr}? Commented Jan 17, 2014 at 16:07
  • 1
    frame is an array whose length is determined at compile time. As yourself how the compiler can do that, and meet your needs. Commented Jan 17, 2014 at 16:08
  • 1
    @enedil That wouldn't work at all, it wouldn't even compile. Commented Jan 17, 2014 at 16:08
  • If "Hello" is passed as a char *, what is going to happen trying to pick it up as a uint8_t * in the function? Do you pass in a pointer to a long enough space to hold all the data? (a single 64 bit integer should be enough, but what are you doing with an array?) I'm concerned about the { } constant array -- will it be updated with Ptr or *Ptr at run time? Commented Jan 17, 2014 at 16:14

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.