Skip to content

Instantly share code, notes, and snippets.

@pavly-gerges
Last active February 25, 2023 06:55
Show Gist options
  • Save pavly-gerges/e8bbae849adc46975ae2860b6f8511ec to your computer and use it in GitHub Desktop.
Save pavly-gerges/e8bbae849adc46975ae2860b6f8511ec to your computer and use it in GitHub Desktop.
Tests using pointer operations on a data structure to evaluate its members.
/**
* @brief Tests using pointer operations on a data structure to evaluate its members.
*
* @author pavl_g
*/
#include <stdio.h>
struct Test {
int x;
int y;
int z;
};
int main() {
struct Test test;
int* ptr = (int*) &test;
*(ptr) = 2;
*(ptr+1) = 3;
*(ptr+2) = 5;
printf("%lld\n", (test.x));
printf("%lld\n", (test.y));
printf("%lld\n", (test.z));
printf("%lld\n", *((int*) &test));
printf("%lld\n", &(test.x));
printf("%lld\n", &(test.y));
printf("%lld\n", &(test.z));
return 0;
}
@pavly-gerges
Copy link
Author

Output:

/tmp/DkO3Sh7ycx.o
2
3
5
2
140731634115948
140731634115952
140731634115956

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment