4 ms·
struct { char somechar; char somearray[7]; } foo = {'E', "xample"}; printf("%c\n", foo.somearray[-1]); // prints 'E'
by tesseract 17y ago
struct {
char somechar;
char somearray[7];
} foo = {'E', "xample"};
printf("%c\n", foo.somearray[-1]); // prints 'E'
// (depending on your compiler and the
// struct-packing directions you gave it)
- NathanKP 17y ago(depending on your compiler and the directions you gave it) I don't think I was trust this piece of code in a commercial application. There is too much variation which would make it unreliable across platforms and compilers.
- tesseract 17y agoThe negative array index here is a bit perverse, granted. But the general idea of using structs (with ideally controlled, but hopefully at least well-known, packing) is surprisingly common, in my experience. I've seen it used for defining binary file and wire formats (which is perhaps somewhat dodgy but there you have it) and, perhaps more appropriately, for EEPROM layouts and the like in embedded systems.