Error-prone knowledge points in C language

Source: Internet
Author: User

1. Calculation of structural bodies

struct Bbb{//long a;//char c1;//char c2;//long b;//long c;//}*p;//sizeof (struct BBB) =16;//int main ()//{//p = (struct BB b*) 0x100000;//printf ("0x%x", p + 0x1),//plus the entire structure size is 0x10010//printf ("0x%x", (unsigned long) p + 0x1);//integral type plus 1,0x100001// printf ("0x%x", (unsigned long*) p + 0x1);//plus sizeof (unsigned long) *, for 0x100004//printf ("0x%x", (char *) p + 0x1);//Plus size of (Char) * *, for 0x100001//system ("pause");//return 0;//}

2. Structure structure, the size of the structure in the common 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;//};//knot The structure is a whole in 4 aligned 6+4=10,short and the largest occupied byte in Long is the integer multiples of 12//int main ()//{//printf ("%d%d%d\n", sizeof (union AAA), sizeof (struct BBB), sizeof (struct TAGCCC));//system ("pause");//return 0;//}

Execution Result:

1-byte alignment: 4 6 8

4-byte Alignment: 6 8 12

3. If there is no break in the case statement, then the statement after it will be executed.

4. Pointers and const declarations:

(1) The object referred to by the pointer is readable

const int *p;

int const *P;

(2) The pointer is readable

int *const p;

(3) Pointers and pointers refer to objects that are readable

const int * const P;

int const *CONSTP;

5. Dynamic opening up

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 the call function P dynamic opening and the main function p is not the same place, because P is only a temporary copy, no address passed.

b.strcpy, there was an error, and P is not pointing at this time.

C.free does not open it (temporary variable) and should have the pointer empty 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; }


This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1698559

Error-prone knowledge points in C language

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.