0
    Struct some{
         unsigned int a:5;
         Unsigned int b:6;
    } std1;

Now let us have stored some values in std1. Now how can we display the bit pattern of the structure variable std1 in C?

2
  • Thank you for the immediate reply, can you please provid me the code for how to copy std1 to the union by assigning to the struct some member. Commented Nov 12, 2020 at 9:47
  • also i'm restricted to use any other header files then stdio.h,stdlib.h and malloc.h,so uint8_t* provide an error. thank you Commented Nov 12, 2020 at 9:50

1 Answer 1

1

For any data type in C, you can inspect its raw binary contents by iterating over it using character type pointers. Example:

const uint8_t* ptr = (const uint8_t*)&std1;
for(size_t i=0; i<sizeof std1; i++)
{
  printf("%.2X ", (unsigned int)ptr[i]);
}

(uint8_t will always be a character type in all real world systems except on some dysfunctional legacy DSPs that shouldn't be used for any purpose.)

Sign up to request clarification or add additional context in comments.

3 Comments

As iam limited to use the stdlib.h,stdio.h and malloc.h. It shows an compiler error,can you please tell me any other method .Thanks for the help @Lundin
@AndrewAllen I didn't test this code. Did you include stdint.h? Otherwise, if that's not possible, simply switch uint8_t for unsigned char.
no i didn't include the stdint. then i use unsigned char instead of uint_8 and i got the hexadecimal values as output(is that my ans?) @lundin Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.