Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6553655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:36:28+00:00 2026-05-25T12:36:28+00:00

I need to write data into a structure where the length of the data

  • 0

I need to write data into a structure where the length of the data depends on the command I want to send to a device. For that I have defined the following structure:

typedef struct {
    uint8 len;          // Command length (cmd ... crc)
    uint8 cmd;          // Command code
    uint8 data_length;  // Data length
    uint8 data[12];     // Data: max 12 Byte
    uint8 crc_h;        // CRC value MSB
    uint8 crc_l;        // CRC value LSB
}CMD_TYPE;

Note: the members cmd, *data_length* and crc that are always present, instead member data can be empty or contains up to 12 Bytes.

I have created a function that returns a initialized command according to the parameters passed to the function:

CMD_TYPE Device::get_cmd(uint8 cmd, uint8 data_len, uint8 *data)
{
    CMD_TYPE cmd;

    cmd.len = (4 + data_len) * sizeof(uint8);
    cmd.cmd = cmd;
    cmd.data_length = data_len;
    cmd.data = (uint8 *)realloc(cmd.data, data_len*sizeof(uint8));
    if(data_len > 0)    memcpy(cmd.data, data, data_len);

    add_crc16((uint8*)&cmd);

    return cmd;
}

The function get_cmd() is used like this:

uint8 cmd_code = 0x01;
uint8 data[2] = {0xAB, 0xCD};

CMD_TYPE cmd = local_device->get_cmd(cmd_code, 2, data);
retVal = local_device->send(cmd);

When I try to compile this code I get an error from the compiler for that line:

cmd.data = (uint8 *)realloc(cmd.data, data_len*sizeof(uint8));

and the compiler error is:

error: lvalue required as left operand of assignment

The aim of using realloc() is to re-size the array data or to remove it at all from my new command structure. What is wrong in my code? Is that the right way to initialize structures with dynamic memory allocation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:36:29+00:00Added an answer on May 25, 2026 at 12:36 pm

    What you want is the infamous struct hack:

    typedef struct
    {
        uint8   len;          // Command length (cmd ... crc)
        uint8   cmd;          // Command code
        uint8   data_length;  // Data length
        uint8   crc_h;        // CRC value MSB
        uint8   crc_l;        // CRC value LSB
        uint8   data[1];      // Data: max 12 Byte
    } CMD_TYPE;
    

    The trick is to allocate enough room for all of the members of the struct up to data[], then add enough bytes for the data[] member:

    CMD_TYPE * allocCmd(int dataSize)
    {
        int         len;
        CMD_TYPE *  p;
    
        len = sizeof(CMD_TYPE) + (dataSize-1)*sizeof(uint8);
        p = (CMD_TYPE *) malloc(len);
        memset(p, 0, len);
        p->data_length = dataSize;
        return p;
    }
    

    Here, len is calculated to be the size of the struct, minus the size of the empty data member, plus however many elements dataSize specifies for the data array.

    The catch is that you have to be careful never to access any elements of p->data[] beyond what is actually allocated in it (inside the struct).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write data into drive. I have two options: write raw sectors.(_write(handle,
I have to write a component that re-creates SQL Server tables (structure and data)
I need to write some data in several database. I choose sqlapi.com I have
I need to read and write some data through named pipes. I have tested
I need to write a module that sends order data to an epayment service,
I need to write an app that retrieves data on expiring SSL certs. What
I have following question. I'd like to place a file named data.xml into sdcard/appname
I'm trying to properly write code to build a data structure to serialize into
I want to write a stored procedure that queries XML files after I have
I have an XML structure like the following: <tables> <table name=tableName1> <row ID=34 col1=data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.