Serialized Data Buffer

Code

#include <stdio.h>
#include <stdint.h>

void build_packet(uint8_t command, uint16_t value, uint8_t status, uint32_t checksum) {
    uint8_t buffer[10];

    uint8_t *ptr = buffer; //points to the starting of the buffer array

    *ptr = 0xA5; //start, index =0
    *++ptr = command; //command, index = 1
    *++ptr = value & 0xFF; //take the lsb, index =2
    *++ptr = (value>>8) & 0xFF; //take the msb, index = 3
    *++ptr = status; //status, index = 4
    *++ptr = checksum & 0xFF; //checksum lsb, index =5
    *++ptr = (checksum >> 8) & 0xFF; // index = 6
    *++ptr = (checksum >> 16) & 0xFF; //index = 7
    *++ptr = (checksum >> 24) & 0xFF; //index = 8
    *++ptr = 0x5A; //index = 9

    uint8_t *print = buffer; //poimts to starting of buffer again
    volatile uint8_t i;
    
    for(i=0;i<10;i++){
        printf("%d ", *print++);
    }
    
    // Your logic to fill buffer
    // Then print buffer
}

int main() {
    uint8_t cmd, status;
    uint16_t val;
    uint32_t crc;

    scanf("%hhu %hu %hhu %u", &cmd, &val, &status, &crc);
    build_packet(cmd, val, status, crc);
    return 0;
}

Solving Approach

Initialization:

*++ptr : is updating the address then we initalize value to that new address

*ptr++: updates after printing current value pointed to pointer

 

 

Upvote
Downvote
Loading...

Input

1 4660 1 2864434397

Expected Output

165 1 52 18 1 221 204 187 170 90