#include <stdio.h> #include <string.h> int main() { { char *str1 = "hello!"; char *str2; printf("str1(%d): %s\\n", strlen(str1), str1); str2 = str1; printf("str2(%d): %s\\n", strlen(str2), str2); printf("please input a character -> "); // *str2 = getchar(); // Segmentation fault here printf("modified str2(%d): %s\\n", strlen(str2), str2); }printf("\\n"); { int a = 65; char *b = &"ABC"; int *c = &"ABC"; printf("a=0x%x, *b=0x%x, *c=0x%x\\n", a, *b, *c); printf("&a=0x%x, b=0x%x, c=0x%x\\n", &a, b, c); printf("&\\"ABC\\" is 0x%x\\n", &"ABC"); printf("int value of \\"ABC\\" is 0x%x\\n", *((int *)&"ABC")); }printf("\\n"); { typedef struct { char a; char b; int c; } user_unpacked_type; #pragma pack(push, 1); typedef struct { char a; char b; int c; } user_type; #pragma pop(); printf("size of user_unpacked_type is %d\\n", sizeof(user_unpacked_type)); printf("size of user_type is %d\\n", sizeof(user_type)); { int i; user_type ut = {'A', 'B', *((int *)&"CDE")}; for(i=0; i<sizeof(user_type); i++) printf("ut[%d] is 0x%x\\n", i, ((char *)&ut)[i]); printf("or otherwise....\n"); for(i=0; i<sizeof(user_type)-sizeof(int); i++) printf("ut[%d] is 0x%x\\n", i, ((int *)&ut)[i]); } }printf("\\n"); { unsigned int a = 1234567890; int b = *((int *)&a); char c = *((char *)&a); long d = *((long *)&a); printf("a = 0x%x\\n", a); printf("b = 0x%x\\n", b); printf("c = 0x%x\\n", c); printf("d = 0x%x\\n", d); }printf("\\n"); return 0; } |
// first running str1(6): hello! str2(6): hello! please input a character -> modified str2(6): hello! a=0x41, *b=0x41, *c=0x434241 &a=0x1ae753b0, b=0x4009c7, c=0x4009c7 &"ABC" is 0x4009c7 int value of "ABC" is 0x434241 size of user_unpacked_type is 8 size of user_type is 6 ut[0] is 0x41 ut[1] is 0x42 ut[2] is 0x43 ut[3] is 0x44 ut[4] is 0x45 ut[5] is 0x0 or otherwise.... ut[0] is 0x44434241 ut[1] is 0x45 a = 0x499602d2 b = 0x499602d2 c = 0xffffffd2 d = 0x499602d2 // second running str1(6): hello! str2(6): hello! please input a character -> modified str2(6): hello! a=0x41, *b=0x41, *c=0x434241 &a=0xd94056b0, b=0x4009c7, c=0x4009c7 &"ABC" is 0x4009c7 int value of "ABC" is 0x434241 size of user_unpacked_type is 8 size of user_type is 6 ut[0] is 0x41 ut[1] is 0x42 ut[2] is 0x43 ut[3] is 0x44 ut[4] is 0x45 ut[5] is 0x0 or otherwise.... ut[0] is 0x44434241 ut[1] is 0x45 a = 0x499602d2 b = 0x499602d2 c = 0xffffffd2 d = 0x499602d2 |