ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've tried to make a pointer in order to read the data of the structure byte-by-byte, but no matter what I do I get "incompatible pointer type". So what should I do?
You can explicitly cast one pointer to another of different type by doing something like this and use the new pointer to process whatever that is in there. char *ptr=(char *)&data;
Unfortunately in your example it will run into some problem because the stepdata structure is not well-aligned. GCC will pad bytes to make 32bit int's and struct boundary aligned to 32bit unless you ask it not to do so with __attribute__((packed)).
One more bit of info about your packing problem, if you choose to force byte packing of that structure you will get a "Bus Fault" if you compile and run your software on any processor other than Intel X86 because the 16 and 32 bit integers will not be word aligned. For example, it will crash on a Sparc.
i.e... struct stepdata data={1,2,3,4,5,6,7,8,0}; **bus fault**
But it will work on Intel.
In x86 it can get away from crashes, but with performance penalty. To avoid this you should
1) manually pad extra bytes so that int16 words and int32 dwords are 16bit and 32bit aligned respectively, or
2) define dedicated function to perform the checksum over this particular structure type
The following one is an attempt to
A) align int16 and int32 variables., and
B) keep struct size multiple of 32bit.
(If you don't do B, once you have an array of this struct, the 2nd one may have everything mis-aligned.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.