Error-prone knowledge points in C Language

Source: Internet
Author: User
Tags case statement

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;  }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.