-5

I'm having a problem trying to save a data of uint64_t size into 4 uint16_t positions in array without using any loop... Here is a part of my code:

static int send(uint16_t addr, const void *data)
{
    uint16_t frame[7];
    /* Here I want to save in frame[2], frame[3], frame[4] and frame[5] the data recieved by parameter */

}

Thanks in advance! :)

OriolBur
  • 11
  • 6
  • 3
    And what exactly is the problem you are having? – UnholySheep May 23 '18 at 08:58
  • Honestly, I don't know how to do it... – OriolBur May 23 '18 at 09:00
  • Well, what have you tried doing? What has your research suggested as solution(s)? How have those not worked for you? – UnholySheep May 23 '18 at 09:03
  • I was trying to do it with this line : memcpy(&frame[2], data, 8); , but without success – OriolBur May 23 '18 at 09:05
  • 3
    What does *"without success"* mean? What happened and what did you expect to happen? Provide a proper [mcve] please, right now it's still unclear what exactly your problem is – UnholySheep May 23 '18 at 09:07
  • 1
    Ok, sorry. If data is 0x0000000000000004, I expect to save into array this: frame[2] = 0x0004, frame[3] = 0x0000, frame[4] = 0x0000, frame[5] = 0x0000. "Without success" means that I get a crash in my program : Access violation reading location 0x00000000 – OriolBur May 23 '18 at 09:12
  • 3
    First of please don't post relevant information into comments, [edit] it into your question. Second off we still require a complete [mcve], ideally you should be able to reduce your code to a self-contained example. (e.g.: right now we neither now the size of `MCB_FRAME_SIZE` or where this null pointer access comes from) – UnholySheep May 23 '18 at 09:15
  • The green checkmark is [so]'s equivalent of "Thanks you so much!" comments on other sites. Here, comments should only be used for suggesting improvements, for the other usage you have the checkmark. See https://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts and https://meta.stackoverflow.com/questions/258004/should-thank-you-comments-be-flagged – user202729 May 23 '18 at 09:21
  • 1
    You appear to have some difficulties with asking questions on [so]. [Stack Overflow question checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) may be helpful. Although... – user202729 May 23 '18 at 10:03
  • for this particular question, you should be able to receive better answers by showing your code (the code which give you the access violation) as an MCVE, and redo the steps above. – user202729 May 23 '18 at 10:05

1 Answers1

0
typedef union
{
    uint64_t u64;
    uint32_t u32[2];
    uint16_t u16[4];
    uint8_t u8[8];
}UINT_UNION_T;

uint16_t *saveU64(uint16_t *table, size_t position, uint64_t value)
{
    UINT_UNION_T u = {.u64 = value};

    table[position] = u.u16[0];
    table[position + 1] = u.u16[1];
    table[position + 2] = u.u16[2];
    table[position + 3] = u.u16[3];
    return table;
}

Or

  memcpy(table + position, &value, sizeof value);
0___________
  • 60,014
  • 4
  • 34
  • 74
  • 1
    Not really a good idea since this needlessly turns the code endianess-dependent. It is better to only use bit shifts. – Lundin May 23 '18 at 11:00
  • @Ludlin it is not. It is not required here to have the lower bytes in the lower uint16_t table indexes. Typical nitpick. – 0___________ May 23 '18 at 11:37
  • 1
    So how do you know what's inside `u.u16[0]`? You don't, unless you assume a certain endianess and hard-code accordingly. – Lundin May 23 '18 at 11:45