Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Various issues to be a covered in your question. I'll take a shot at some of those issues.</p> <ul> <li>The order of members in a structure is guaranteed to be the same as order you have declared them. But there is a different issue here - padding. Check this -<a href="http://c-faq.com/struct/padding.html" rel="nofollow">http://c-faq.com/struct/padding.html</a> and follow other links/questions there</li> <li><p>Next thing is that you are mistaken in thinking that something like "125" is an integer or something like "1.25" is a float - it's not - it's a string. i.e. </p> <pre><code>char * p = "125"; </code></pre> <p>p[0] will not contain 0. It will contain '0' - if the encoding is ASCII, then this will be 48. i.e. p[0] will contain 48 &amp; not 0. p[1] will contain 49 &amp; p[2] will contain 52. It will be something similar for float.</p></li> </ul> <p>The opposite will also happen. i.e. if you have at an address and you treat it as a char array - the char array will not contain the float you think it will. Try this program to see this</p> <pre><code>#include &lt;stdio.h&gt; struct A { char c[4]; float * p; int i; }; int main() { float x = 1.25; struct A a; a.p = &amp;x; a.i = 0; // to make sure the 'presumed' string starting at p gets null terminate after the float printf("%s\n", &amp;a.c[4]); } </code></pre> <p>For me, it prints "╪·↓". And this has nothing to do with endianness. </p> <ul> <li>Another thing you need to remember, while assigning values to your structure object - you need to remember that comStr.pressure &amp; comStr.temperature are pointers. You cannot assign values to them directly. You need to either give them the address of an existing float or allocate memory dynamically to which they can point to.</li> </ul> <p>Also are you trying to create the char array or to parse the char array which already exists. If you are trying to create it, a better way to do this will be to use <code>snprintf</code> to do what you want. <code>snprintf</code> uses format specifiers similar to <code>printf</code> but prints to a char array. You can create your char array that way. A bigger question remains - what do you plan to do with this char array you create - that will determine if endianness is relevant for you.</p> <p>If you are trying to read from the char array you have been given and trying to split into floats and commas and whatever, then one way to do this will be <code>sscanf</code> but may be difficult for your particular string format.</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.
    1. 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