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 9128501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:31:45+00:00 2026-06-17T07:31:45+00:00

I have a C structure like this … struct icmp_prefixopt { u_int8_t icmpopt_type; u_int8_t

  • 0

I have a C structure like this …

struct icmp_prefixopt {
    u_int8_t        icmpopt_type;
    u_int8_t        icmpopt_len;
    u_int8_t        prefixlen;
    u_int8_t        lflag:1;
    u_int8_t        aflag:1;
    u_int8_t        reserved:6;

};

and I have provided values to members like this in same module-

   popt= (struct icmp_prefixopt *)
                    malloc(sizeof(struct icmp_prefixopt));

  popt->icmpopt_type = 3;
  popt->icmpopt_len = 4;
  popt->prefixlen = (u_int8_t)strtoul(arg, (char **)NULL, 0);

     arg = index(arg, '+');
            if (arg) {
                    ++arg;
                    popt->lflag = ((u_int8_t)strtoul(arg, (char **)NULL, 0))&1;
            }


     arg = index(arg, '+');
            if (arg) {
                    ++arg;
                    popt->aflag = ((u_int8_t)strtoul(arg, (char **)NULL, 0))&1;
            }


     arg = index(arg, '+');
            if (arg) {
                    ++arg;
                 popt->reserved = 32;  //((u_int8_t)strtoul(arg, (char **)NULL, 0))<<2;
            }

where arg is command line argument passed to this module.

Now looking at the contents of structure after execution in hex format ->

  03 04 20 81

   icmpopt_type: seems fine
   icmpopt_len: seems fine
   prefixlen: seems fine

but bits looks like reversed for other 3 fields in their constitute byte-

  lflag:1; aflag:1; reserved:6

so it should have been – 10100000=A0 but actually they are =>81=10000001

It arise many questions to me …

  1. Is there anything to do with little endian/big endian?

  2. If yes, what is the counterpart for functions like htonl and htons for 8 bit.

  3. If no, what may be the possible issue or have I misunderstood something completely ?

  4. What is best approach ? To modify order of these fields within the structure
    itself or applying some bit wise operator and shifting of bits here itself?

The input provided at command line-

    32+1+0+32 

This final 32 serves no purpose here,as I have fixed 32 in module itself for testing.
Although my actual purpose needs to consider this field also.

Please help me soon with any alternative approach.

Thanx in advance.

Edit:

enter image description here

This is the actual structure I need to create and along with creating, need to made a provision for users to specify values for all the fields through GUI. (Right now only through linux command line).

I guess I have made the problem more clear now but still if any further information is required, I would be much happy to add.

  • 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-06-17T07:31:47+00:00Added an answer on June 17, 2026 at 7:31 am

    How the compiler chooses to pack bit-fields is completely implementation-dependent. It doesn’t necessarily have anything to do with endianness.

    htnol (and similar) don’t apply to bit-fields. If you need a guaranteed order, then you will need to manually pack a uint8_t yourself. For example:

    struct icmp_prefixopt {
        u_int8_t        icmpopt_type;
        u_int8_t        icmpopt_len;
        u_int8_t        prefixlen;
        u_int8_t        stuff;
    }
    
    ...
    
    popt->stuff = (lflag << 7) | (aflag << 6);
    

    Of course, in practice, you should use sensible #defines rather than magic numbers (for 6 and 7). And you may decide to wrap this in a bunch of setter and getter functions.

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

Sidebar

Related Questions

I have a structure like this struct structure { BaseObject &A; //BaseObject has a
I have a structure like this struct list { struct list *next; int temp;
I have a structure like this. struct A { int someFun() const; int _value;
I have structure like this: struct Rz3DContourNode { float x; float y; float z;
I have a structure like this typedef struct{ int stat; }dot; And I would
I have structure like this below. Now, I want swap 2 structures. public struct
I have seen structure declarations which looks like this one typedef struct br {
I have structure like this. <ul class=gallerylist> <li> <image ...> <input ...> </li> <li>
I have a structure like this: var var1 = { array1 : ['value1','value2', ...],
I have a structure like this.... UITableViewController -> UITableViewCell -> UIView I need the

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.