3

I have this array

uint8_t *buffer = "JOHN:DOE:010119:M:FOO:BAR";

and I want to copy it field by field to data structure

typedef struct{
  uint8_t firstName[5];
  uint8_t pad1;
  uint8_t lastName[4];
  uint8_t pad2;
  uint8_t dateOfBirth[7];
  uint8_t pad3;
  uint8_t genre;
  uint8_t pad4;
  uint8_t car[4];
  uint8_t pad5;
  uint8_t phone[4];
  uint8_t pad6;
}DataStructTypeDef;

Let's say that all lengths are fixed (eg. firstName is always composed of 4 characters, lastName of 3 etc ...)

I used this approach:

DataStructTypeDef foo;
memcpy((void *)&foo, (void *)buffer, sizeof(DataStructTypeDef));

When I try to print dateOfBirth it shows the whole array starting from 01012019 like this

int main(void)
{
  DataStructTypeDef foo;
  memcpy((void *)&foo, (void *)buffer, sizeof(DataStructTypeDef));
  printf("%s", foo.dateOfBirth); // It prints 010119:M:FOO:BAR
//printf("%s", foo.dateOfBirth); // Expected value 010119
  return 0;
}
12
  • 1
    Warning : "firstName is always composed of 4 characters,lastName of 3 etc ..." is false, you missed the place for the null character ending them
    – bruno
    Commented Jan 22, 2019 at 14:20
  • 2
    @zerocoldTUN if you do not end the strings by a null charac you will not be able to use the standard functions supposing that by is present, so no printf / strcpy / strdup/ .... uint8_t lastName[3]; means lastName contains 1 or 2 characters then the \0, not 3 characters
    – bruno
    Commented Jan 22, 2019 at 14:24
  • 2
    What is your question? Are you aware of how C mimiks "string"s, which in fact are no data type in C. You might like to read about the concept of 0-terminated char-arrays.
    – alk
    Commented Jan 22, 2019 at 14:45
  • 2
    "using a padding bit" what you call a "bit" in fact is a byte, or to be more precise a unit8_t.
    – alk
    Commented Jan 22, 2019 at 14:56
  • 1
    Note: Casts are not needed in C for this code, Suggest using sizeof object. Simplification: memcpy(&foo, buffer, sizeof foo); Easy to code right, review and maintain. Commented Jan 22, 2019 at 16:15

4 Answers 4

6

Since the char array members you are copying are not null terminated, printf("%s", will not know when it has encountered the end of each string.

This can be controlled in printf by limiting the amount of characters that print...

For example:

printf("%.*s", (int)sizeof(foo.dateOfBirth), foo.dateOfBirth);

An equivalent would be:

printf("%.6s", food.dateOfBirth);

.* specifies the "precision" of characters you want to print. So in your case, dateOfBirth = precision/size 6.

1
  • %.*s specifies the maximum number of characters to output. output will stop at a null terminator if one is present before this limit.
    – chqrlie
    Commented Jan 22, 2019 at 18:20
2
  1. Modify your structure

Add an extra byte to each field to accommodate the '\0' character. For e.g. use

uint8_t firstName[5];

instead of

uint8_t firstName[4];
  1. Parse the fields individually and end each field with '\0'

Instead of copying the entire buffer in one go, copy the elements one by one. Since, the size of each field is fixed, the offset from the start of the buffer is fixed and this makes the job of parsing easier.

2

With the fixed struct of

typedef struct {
    uint8_t firstName[4];
    uint8_t pad1;
    uint8_t lastName[3];
    uint8_t pad2;
    uint8_t dateOfBirth[6];
    uint8_t pad3;
    uint8_t genre;
    uint8_t pad4;
    uint8_t car[3];
    uint8_t pad5;
    uint8_t phone[3];
    uint8_t pad6;
}DataStructTypeDef;

This works for me:

int main(void)
{
    uint8_t *buffer = "JOHN" "\0" "DOE" "\0" "010119" "\0" "M" "\0" "FOO" "\0" "BAR";
    DataStructTypeDef foo;
    memcpy((void *)&foo, (void *)buffer, sizeof(DataStructTypeDef));
    printf("%s", foo.dateOfBirth); // Expected value 01012019
}

The buffer looks horribly mangled because if I put "\0" "010119" as "\0010119", it's interpreting the escape the wrong way. A better solution would probably be to keep it as one and fully write out the octal sequence as \000:

uint8_t *buffer = "JOHN\000DOE\000010119\000M\000FOO\000BAR";

Here, each \000 becomes a null byte and it doesn't clash with the 010119 following one of the escape sequences.

Alternatively, it works if I take the original buffer string of "JOHN:DOE:010119:M:FOO:BAR" and just replace all the : after copying, like this:

foo.pad1 = foo.pad2 = foo.pad3 = foo.pad4 = foo.pad5 = foo.pad6 = '\0';
8
  • Are you really trying to initialize the scalar uint8_t pad1 field with the string "\0"? Commented Jan 22, 2019 at 14:50
  • 1
    @AndrewHenle no, those strings get automatically concatenated into one string at compile time. So all those "\0" translate to one \0 each. I only did it this way because otherwise it would interpret it as a different literal when there's a a 0 right after a \0 in the literal.
    – Blaze
    Commented Jan 22, 2019 at 14:52
  • Ah. Got it. I didn't catch the missing , between the fields. Commented Jan 22, 2019 at 14:53
  • 1
    Re “it's interpreting the escape the wrong way”: In C, an octal escape sequence has one to three octal digits. The compiler correctly interprets ”\001" as 1. Zero can be designated with ”\000". Commented Jan 22, 2019 at 15:51
  • 1
    @alk: no, only a final "\0" would translate to 2 null bytes. concatenated strings do not have extra embedded null bytes, only the ones that appear explicitly.
    – chqrlie
    Commented Jan 22, 2019 at 18:23
1

After memcpy add this: foo.pad1 = foo.pad2 = foo.pad3 = foo.pad4 = foo.pad5 = 0;. But I hope this is exercise, not a real structure for real job.

6
  • "I hope this is exercise, not a real structure for real job." What's wrong with it?
    – Pryda
    Commented Jan 22, 2019 at 14:36
  • @zerocoldTUN For example: 1) do you expect to use only names where first name is always 4 letters and last name always 3 letters? 2) Instead of pad1 and pad2 you can expand firstName and lastName with 1 byte for trailing zero. 3) It is normal to use char array for name, not byte array.
    – i486
    Commented Jan 22, 2019 at 14:40
  • In fact that was just an example. I am not treating first names and last names. In fact I have an EEPROM (memory) and I am using in it to store data for my product. Product ID, Manufacture date, Batch number etc ... Reading data from the memory returns an array of characters, and I am the one who defined those fields. I am using fixed-length fields for each data for example: the date is always DDMMYY and January, 1st is not written like this: 1119. It's always 010119. So yeah: I am 100% sure that all field sizes are fix and that's why I wanted to store it directly to data structure.
    – Pryda
    Commented Jan 22, 2019 at 14:49
  • 2
    @zerocoldTUN: "100% sure that all field sizes are fix" What the use of the colons (:) inside the data, though?
    – alk
    Commented Jan 22, 2019 at 14:54
  • 1
    @zerocoldTUN: '0' and \0 are not the same.
    – alk
    Commented Jan 22, 2019 at 14:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.