Intermediate C Language

Source: Internet
Author: User

Summary of intermediate C language knowledge points:

1. sizeof keywords:

Determined during compilation, sizeof (object), sizeof (type), sizeof object, sizeof (pointer) = 4.

Int a [5]; sizeof (a) = 20; sizeof (& a) = 20;

2. Empty struct cannot be defined in c; c ++ is allowed, sizeof () = 1

3. char * a = "hello" "world"; // a is "helloworld"

Pintf ("\ 32" "3 \ n") = printf ("\ 323 ");

Printf ("\ 172 \ n"); // 172 ascii Value

Printf ("\ 0x60"); // hexadecimal 60 h ascii Value

Printf ("\ 60" "3 \ n"); // 03

4. a [0] = * (a + 0) is an expression. The value 0 [a] is also a line. a represents the first address of the first element of the array.

Int a [10]; & a + 1 points to the end of a [9], and the whole row is incremented by one.

Addressing the array name & a will increase the array dimension and change to the array pointer int (*) [10].

5. Calculate the number of signed and unsigned numbers, and convert the result to the number of unsigned.

Static int I = 10; specifies that I is used in this file

Extern int a;/extern a; declare a from other files

6. Role of const

Const int x = 2; or int const x = 2;

Const int * a or int const * a; // a is variable, and the object a points to is immutable;

Int * const a; // const modifies pointer a, and pointer a is immutable.

Const can modify input parameters, return values, and member functions.

7. const pointer

A pointer to a const object cannot be assigned a pointer to a non-const object.

(Non-const can be assigned to const)

(1). const int I = 10;

Const int * p = & I; // p points to const int

Int * q = p; // invalid

(2). const int I = 10;

Const int ** p1;

Int * p2;

P1 = & p2;

* P1 = & I; // equivalent to p2 = & I, but p2 = & I is invalid.

* P2 = 20; // The constant const int is modified. The specific result is tested in the environment.

8. typedef int array [10]; // typedef int [10] array;

Array;

Array B [5]; // B [5] [10]

Array * c; // (* c) [10], (* c) [10] use a two-dimensional array to assign a value, ++ moves a row down

Array * c [4]; // int (* c [4]) [10], pointing to the pointer array of the Two-dimensional array

A in the one-dimensional array int a [10] is equivalent to defining a pointer to an element ---> int * const;

B of the Two-dimensional array int B [3] [4] is equivalent to defining a pointer to the one-dimensional array as an element ---> int (* const B) [4]

Int B [3] [5];

* (B + 2) + 0) values of column 0th in the second row of B;

* (B + 2) the first address of the second line in B. * () has the function of dimensionality reduction.

B + 2 is the address of B [2] [0], type: int [] [5], without Dimensionality Reduction

9. Structure alignment rules

(1). The first address of the variable in the struct can be divisible by the smaller member size and alignment base;

(2) The total size of the struct is an integer multiple of the size of the widest basic type of the struct and the smaller member in the alignment base. Fill in the case of insufficiency.

(3) determine the offset position and the hour, and treat the composite type (such as struct) as a whole.

Struct a struct B

{Int ii; {int I;

Double dd; struct;

} Aa; double d;

Sizeof (aa) = 16;} B;

Sizeof (bb) = 32;

10. scanf

When scanf is entered in % c format, both spaces and escape characters are valid;

Printf ("%") is required for output %; \ a: % [character set] % [^ \ n] % I

11. Constants

# Define const literal constant Enumeration

12. header files <> and ""

<> You can find it directly in the default system path. If no link is found, a link error is returned;

"" First found in the current path and then in the system path. If not found, a link error is returned.

13. data types are determined: (1). memory space used by data; (2). Storage Format; (3). calculation rules-Behavior

The storage type determines the storage region, and the region determines the scope and lifecycle.

14. Memory Partition

The c ++ memory is divided into five areas: heap (new), stack (local variables, function parameters), Global/static storage area, free storage area (malloc), and constant area.

The C language is divided into four areas: heap (malloc), stack (local variables, function parameters), Global/static storage area, and constant area.

15. Four Elements of heap memory usage:

Int * p = (int *) malloc (10 * sizeof (int ));

If (null = p) return;

Free (p );

P = null;

16. _ Msize (p); returns the size of the memory heap space.

17. Do not return the pointer and reference pointing to the stack memory. Static local variables can be returned. Static local variables exist but are invisible outside the function.

18. typedef trap

Typedef char * pstr;

Define const pstr. We want it to be const char *, but it is actually char * const

19. pointer

(1). No type pointer void * p. You cannot add or subtract an integer. You can only perform values, comparisons, and sizeof operations.

(2) static local variable pointers must be initialized using global variables;

(3). pointers of different types cannot be evaluated. The pointer difference is (number of bytes/type) sizeof (& a [5]-& a [0]) = 4;

(4). int a [10]; a is a constant and an address; vc sizeof (& a) = 40; gun sizeof (& a) = 4;

(5). int a [8] [9]; int (* p) [9] =;

(6). char * p = "abcd"; p [0] = 'M'; // invalid. p points to the constant area and cannot be modified;

(7 ). char * p = "hello"; // incorrect syntax, because p points to the constant area, and p points to the pointer of the variable, which means that the indicated data can be modified, it should be changed to const char * p = "hello ".

(8). The pointer size of any type is 4 bytes.

22. pointer and const

(1) int (2) * (3) p; (1) and (2) position: p points cannot be modified (3) position: p cannot be modified

Char ** p; p ----> char * ----> char;

Const char ** p ----> const char * ----> const ch

Char * const * p ----> char * const -----> char

20. Functions and pointers

The Return Value of the function and the list of form parameters constitute the function type;

Typedef float (* pf) (int, double) // function pointer

Int (* p) [3]; // two-dimensional array pointer, large element, each row has three columns

Int * ptr [3] [4]; // two-dimensional pointer Array

Int * (* ptr) [4]; // pointer to a two-dimensional pointer Array

Int (* myfun () (int *, int *) // defines a function. The return type is a function pointer.

Int (* s [10]) (int) // array containing 10 function pointers

Char (* fun () [4]) (int * p)

----> Char (*) (int * p) (*) [4] fun () // defines a function. The return type is a pointer to a two-dimensional array, the array element is also a function pointer.

Char (* q) () [5]) ()

----> Char (*) [5] (* q) () // defines a function pointer. The return type of the function pointing to is a pointer to a two-dimensional array, the element in the array is also a function pointer.

21. pointer and function parameters

The pointer pointing to the pointer as the form parameter, such as void f (int ** pp), is used to transmit the real parameter as the pointer, such as f (& p );

The function name is an address constant and can be assigned to the corresponding pointer variable ---- function pointer

Function pointer assignment: pfun = function name: The function name is regarded as the function entry address.

Pfun = & function name: regards a function as a name and obtains its address.

Call: (* pfun) (real parameter list)

Pfun (real parameter list)

Void myfun (int x) {printf ("% d", x );}

Void main {

Void (* pfun )();

Pfun = myfun;

Myfun (10); // four call methods are OK

(* Myfun) (10 );

Pfun (10 );

(* Pfun) (10 );

}

22. (1). Do not write an expression in the function call area (in the parameter list;

(2). After ++, after the time to evaluate the value is reached the end of the entire expression, + + in the parameter after the combination of form and reality ++;

(3). The function call result can only be used to initialize non-static variables. static char * p = (char *) malloc (20) ----> error;

(4) static is ineffective and does not affect the shape parameter. static is lost;

(5) Avoid the function from having a memory function (do not use static local variables); do not modify static global variables in the function to avoid affecting other functions;

(6) The function has extern modification by default. The function and global variables can be modified to be limited to this file using static modification;

(7). Two layers of loops should be placed in multiple loops to reduce the number of loops.

23. Preprocessing

(1). the pre-processing statement starts with # and does not contain ";". It occupies an exclusive row and can be placed at any position;

(2). Pre-processing before compilation, including Conditional compilation, macros, and conditions;

(3). Pre-processing is also available before pre-processing: to delete the continued rows, check the function name, and remove comments;

(4) macro definition is not a C statement, not a function, not a type definition. If there is more than one macro value, you must add () to prevent priority errors;

(5). The '#' in the macro makes the variable string;

(6). Use '#' in the macro to connect the variables. # define TEST (a, B) a # B TEST (2, 3); ---> 23;

(7). macro parameters cannot use expressions;

(8). Replace the non-parametric macro with const, and replace the parametric macro With inline;

(9). (void) printf ("Beijing. \ n"); void explicitly discards the returned value.

24. header files

(1). the header file contains repeated variables, which can be solved by Conditional compilation:

# Ifndef _ FILENAME_H _

# Define _ FILENAME_H _

# Endif

(2) do not define large static global variables, such as structure arrays, in the header file;

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.