How to covert a structure to a byte array in C? [duplicate] - c

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
How to convert struct to char array in C
Portable way of writing a C struct to a file (Serialisation for C)
I am looking for converting a structure to byte array in C and i was confused in doing that.Please show me a right way in achieving that. Thanks in advance.

A structure is a byte array. it starts at &mystruct and has the length of sizeof(mystruct_type) bytes.
If the binaries are to long or do contain gaps, check the #pragma pack settings.
hth
Maro

Related

How to read a file backwards in C? [duplicate]

This question already has answers here:
Reading a text file backwards in C
(5 answers)
Closed 7 years ago.
I am reading a one line string from a file and I need to read it into an array in reverse order. How can I accomplish this?(I am using C).
So, my file looks like:
ACGTGCGATCGATCGATCGATATCGATCGTCTGCTTAAGCTC
And I want my array of chars that I read into to look like:
CTCGAA...
Thanks in advance.
Read the file front-to-back into the array, then reverse the contents of the array in place. That's the best way to do that as reading a file backwards is a very slow operation.

I don't know the use colon in C [duplicate]

This question already has answers here:
What does 'unsigned temp:3' in a struct or union mean? [duplicate]
(4 answers)
Closed 7 years ago.
I'm browsing freebsd code and i see this:
i don't understand the use of colon in C
this is an example: u_char ip_hl:4, ip_v:4;
anyone have an idea?
These colons are used to set bit fields of structure members. u_char ip_hl:4 means u_char ip_hl can hold only up to 4 bits.
This is to do with Bit fields. You can learn more about it by reading here

data types to store large numbers in C [duplicate]

This question already has answers here:
Store and work with Big numbers in C
(3 answers)
Closed 8 years ago.
I want to store a number "x" where 0<=x<=(10^18).
Which datatype should be used in C for storing such a large number?
I used "long int" but it's not working..
Use unsigned long long int. It is supported in C99 or later, and as a compiler extension in some pre-1999 compilers. and it must be able to hold at least 1.8 * 10^19 values.

What is a bit vector? [duplicate]

This question already has answers here:
C/C++ Bit Array or Bit Vector
(5 answers)
Closed 9 years ago.
I looked online for a good while now, and can't seem to find a good example of what a bit vector actually is.
I have an assignment to do for college which is to add, remove, union of 2 vectors and the intersection too. But I am struggling to comprehend what an actual bit vector is. I am using C to write this.
Could someone please help me on this, it would be a massive help.
Bit vector is a structure which purpose is an accessability of individual bits. Implementation-wise it can be an integer array with some function provided for addressing and manipulation of individual bits of the array. To the final user the array has to look as a "string" of bits, and the functions have to be able to access arbitrary nth bit of the "string".
There is a bitset class in the standard library of C++ which represent this concept, but I am not aware of some option in C language.

How do we normalize a pointer? [duplicate]

This question already has answers here:
what is meant by normalization in huge pointers
(3 answers)
Closed 9 years ago.
Actually i was studying about huge and far pointers.
I come to know huge pointers are by default in normalized form.
I want to know how can we normalize any pointer?
The huge pointer and far pointer are old concept which live in 16-bit
DOS time. You can search something about DOS programming for more
detail about them.
In 8086 programming (MS DOS), a far pointer is normalized if its offset part is between 0 and 15 (0xF).

Resources