Error-prone knowledge points in C Language
1. Structure Calculation
// Struct BBB {// long a; // char c1; // char c2; // long B; // long c; //} * p; // sizeof (struct BBB) = 16; // int main () // {// p = (struct BBB *) 0x100000; // printf ("0x % x", p + 0x1); // Add the entire struct size to 0x10010 // printf ("0x % x ", (unsigned long) p + 0x1); // integer plus 1, 0x100001 // printf ("0x % x", (unsigned long *) p + 0x1 ); // Add sizeof (unsigned long) * 1, 0x100004 // printf ("0x % x", (char *) p + 0x1 ); // Add sizeof (char) * 1 to 0x100001 // system ("pause"); // return 0 ;//}
2. struct In the struct, sharing the size of the struct in the body
// Union AAA {// struct {// char c1; // short sl; // char c2; //} half; // short kk; //} number; // struct BBB {// char ucFirst; // short us; // char c2; // short uo; //} half; // struct tagCCC // {// struct // {// char c1; // short sl; // char c2; //} half; // long kk; ///}; // The struct is 4-aligned, 6 + 4 = 10. the maximum number of bytes in short and long is 4, and the integer number of 4 is 12 // int main () // {// printf ("% d \ n", sizeof (union AAA), sizeof (struct BBB), sizeof (struct tagCCC )); // system ("pause"); // return 0 ;//}
Execution result: 1-byte alignment: 4 6 84-byte alignment: 6 8 123. If there is no break in the case statement, all subsequent statements will be executed. 4. pointer and const Declaration: (1) the pointer indicates that the object is readable const int * p; int const * p; (2) the pointer is readable int * const p; (3) the pointer and pointer indicate that the object is a readable const int * const p; int const * constp; 5. dynamic Development
void GetMemory(char *p,int len){ p=(char *)malloc(len); } int main() { char *p; GetMemory(p,10); strcpy(p,"bit"); printf("%s",p); free(p); }
The above code has three errors !!! A. At this time, calling function p to dynamically open up is not the same place as the main function p, because p is only a temporary copy and the address is not passed. B. strcpy. An error occurs. In this case, p does not point. C. When free is used, it is not released (temporary variables), and the pointer should be pointed to null after free. The correct code is:
void GetMemory(char **p,int len){ *p=(char *)malloc(len); } int main() { char *p; GetMemory(&p,10); strcpy(p,"bit"); printf("%s",p); free(p); p=NULL; }