Note that there are some explanatory texts on larger screens.

plurals
  1. POContinuous memory allocation with different data type in C?
    primarykey
    data
    text
    <p>I'm trying to compose a string (char array exactly) containing a fixed 14 starting characters and ending with varying content. The varying bit contains 2 floats and 1 32-bit integer that's to be individually treated as 4 1-byte characters in the array separated by commas. It can be illustrated by the following piece of code, which doesn't compile for some obvious reasons (*char can't assign to *float). So, what can I do to get around it?</p> <pre><code>char *const comStr = "AT+UCAST:0000=0760,0020,0001\r"; // command string float *pressure; float *temperature; uint32_t *timeStamp; pressure = comStr + 14; // pressure in the address following the '=' in command string temperature = comStr + 18; // temperature in the address following the 1st ',' in command string timeStamp = comStr + 22; // time stamp in the address following the 2nd ',' in command string </code></pre> <p>I have an unclear memory about something like struct and union in the C language which reserves strictly the memory allocation order in which the variables are defined within the "structure". Maybe something like this:</p> <pre><code>typedef struct { char[14] command; float *pressure; char comma1; float *temperature; char comma2; uint32_t *time_stamp; char CR; }comStr; </code></pre> <p>Does this structure guarantee that comStr-> command[15] gives me the first/last byte (depends on the endian) of *pressure? Or is there any other special structure do the trick hiding from me?</p> <p>(Note: comStr-> command[15] isn't going to be evaluated in future code, so exceeding index boundary is not a concern here. The only important thing here is just whether the memory is allocated continuously so that a hardware fetch lasting for 29 bytes starting from the memory address (comStr-> command) gives me exactly the string I want).</p> <p>p.s. As I am writing this, I came up with an idea. Can I possibly just use memcpy() for the purpose ;) memcpy has parameters of void* type, hopefully it works! I am going to try it now! All hail stackOverflow anyway! </p> <p>EDIT: I should have made myself clearer, sorry for any misleading and misunderstanding! The character array I want to construct is to be sent through UART byte by byte. To do this, a DMA system is to be used to transfer the array to the transmit buffer byte by byte automatically if the character array's starting memory address and length are given to the DMA system. So the character array must to be stored continuously in the memory. I hope this makes the question clearer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload