Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This proposed structure:</p> <pre><code>typedef struct { char[14] command; float *pressure; char comma; float *temperature; char comma; uint32_t *time_stamp; char CR; }comStr; </code></pre> <p>Is not going to help you with your requirement:</p> <blockquote> <p>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 (<code>comStr-&gt;command</code>) gives me exactly the string I want.</p> </blockquote> <p>Note you can't have two members with the same name; you'd need to use <code>comma1</code> and <code>comma2</code> for example. Also, the array dimension is in the wrong place.</p> <p>One problem is that there will be padding bytes within the structure.</p> <p>Another problem is that the pointers will be holding addresses of something outside the structure (since there is nothing valid inside the structure for them to point at).</p> <p>It is not clear what you're after. Only a very limited range of floating point values can be represented by 4 bytes in a string. If you're after binary data I/O, then you can drop the pointers and the commas:</p> <pre><code>typedef struct { char command[14]; float pressure; float temperature; uint32_t time_stamp; }comStr; </code></pre> <p>If you want the commas present, then you're going to have to work harder:</p> <pre><code>typedef struct { char command[14]; char pressure[4]; char comma1; char temperature[4]; char comma2; char time_stamp[4]; char CR; } comStr; </code></pre> <p>You will have to load the data carefully:</p> <pre><code>struct comStr com; float pressure = ...; float temperature = ...; uint32_t time_stamp = ...; assert(sizeof(float) == 4); ... memmove(&amp;com.pressure, &amp;pressure, sizeof(pressure)); memmove(&amp;com.temperature, &amp;temperature, sizeof(temperature)); memmove(&amp;com.time_stamp, &amp;time_stamp, sizeof(time_stamp)); </code></pre> <p>You have to unpack with a similar set of memory copies. Note that you won't be able to use simple string manipulation on the structure; there could be zero bytes in any or all of the <code>pressure</code>, <code>temperature</code> and <code>time_stamp</code> sections of the structure.</p> <hr> <h3>Structure padding</h3> <pre><code>#include &lt;stddef.h&gt; #include &lt;stdio.h&gt; #include &lt;stdint.h&gt; typedef struct { char command[14]; float *pressure; char comma1; float *temperature; char comma2; uint32_t *time_stamp; char CR; } comStr; int main(void) { static const struct { char *name; size_t offset; } offsets[] = { { "command", offsetof(comStr, command) }, { "pressure", offsetof(comStr, pressure) }, { "comma1", offsetof(comStr, comma1) }, { "temperature", offsetof(comStr, temperature) }, { "comma2", offsetof(comStr, comma2) }, { "time_stamp", offsetof(comStr, time_stamp) }, { "CR", offsetof(comStr, CR) }, }; enum { NUM_OFFSETS = sizeof(offsets)/sizeof(offsets[0]) }; printf("Size of comStr = %zu\n", sizeof(comStr)); for (int i = 0; i &lt; NUM_OFFSETS; i++) printf("%-12s %2zu\n", offsets[i].name, offsets[i].offset); return 0; } </code></pre> <p>Output on Mac OS X:</p> <pre><code>Size of comStr = 64 command 0 pressure 16 comma1 24 temperature 32 comma2 40 time_stamp 48 CR 56 </code></pre> <p>Note how large the structure is on a 64-bit machine. Pointers are 8-bytes each and are 8-byte aligned.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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