Summary of C language knowledge points

/*April 24, 2018 17:36:45
author: Arrangement
of TangHui pointer knowledge
*/

/*
1. Pointer:
(the importance of pointer)
represents some complex number structures, transfer data
quickly , reduce memory consumption,
make the function return more than one value//For
example , it is
convenient to directly access the hardware (because the pointer is the address) Handling strings
is fundamental to understanding references in object-oriented languages

Summary: Pointers are the soul of the C language


Definition of pointer:
Address:
the number of the memory unit Non-negative integer starting
from 0
Memory size: (32 lines)
Analysis: 00 01 10 11 4 cases are the case of 2 lines
1k = 2 to the 10th power b
1M = 2 to the 10th power kb = 2 to the 20th power b
1G = 1024M = 2 to the 30th power b
2 to the 32th power = 2 to the 30th power * 2 to the 2nd power = 4G
32 wires are 2 of 32 The unit of power, but the 8 bits of each unit is 32*8 of 2, which is approximately equal to 4G (memory)
range: 4G

pointer:
the pointer is the address, and the address is the pointer.
The pointer variable is the variable that stores the unit number of the memory unit, or the pointer variable is A variable that holds the address.
Pointers and pointer variables are two different concepts,
but it should be noted: usually we refer to pointer variables as pointers when we describe them, but in fact they have different meanings.
The essence of a pointer is a non-negative integer with limited

operation
. Classification of pointers: 1. Pointer of basic type
int *p;//p is the name of the variable, int * means that the p variable stores the address of the int type variable
//int * p; does not mean that a variable named *p is defined
//int *p; should be understood as follows: p is the variable name, and the data type of the p variable is int * type
// The so-called int *; type is to store the address of the int variable of type
int i = 3;
int j;
p = &i;

1.p keeps the address of i, so p points to i
2.p is not i, i is not p, more precisely, modifying the value of p does not affect the value of i, and modifying the value of i does not affects the value of p.
3. If a pointer variable points to an ordinary variable, the
* pointer variable is completely equivalent to an ordinary variable
Example :
if p is a pointer variable, and p stores the address of an ordinary variable i,
then p points to an ordinary variable i
*p It is completely equivalent to i
or: all occurrences of *p can be replaced by i
, and all occurrences of i can be replaced by *p
*p is the variable

j = *p;
printf( "i = %d,j=%d\n",i,j);//3,3


How to modify the value of the ordinary variable of the calling function through the called function
1. The actual parameter must be the address of the ordinary variable
2. The formal parameter must be a pointer variable
3. In the called function, the value of the variable related to the calling function can be modified by means of
*formal parameter name=..... 2. Pointer and array Pointer and one-dimensional array Array name subscript Relationship with pointers Operation of pointer variables Pointers and two-dimensional arrays 3. Pointers and functions 4. Pointers and structures 5. Multi-level pointers Topics :



















Dynamic memory allocation [Key points and difficulties]
Disadvantages of traditional arrays:
1. The length of the array must be specified in advance, and it can only be a constant integer, not a variable
Example:
int a[5];//ok
int len ​​= 5;int a[len ];//error
2. The array defined in the traditional form cannot be manually released by the memory programmer of the array
(during the operation of a function, the space allocated by the system for the array in the function
will always exist until the function finishes running , the space of the array will be released)

3. Once the length of the array is defined, its length cannot be changed. The length of the
array cannot be dynamically expanded or reduced

during the function running. It can be used by other functions during the period,
but after the a function runs, the array in the a function cannot be used by other functions.
Function usage
Arrays defined in traditional ways cannot be used across functions

Why
dynamic memory allocation is required? Dynamic arrays solve the four defects of
traditional arrays well. Traditional arrays are also called static arrays. Examples of

dynamic memory allocation_Structure of dynamic arrays

Static memory and dynamic memory Comparison
Static memory is automatically allocated by the system and automatically released by the system
Static memory is allocated on the stack

Dynamic memory is manually allocated and released by the programmer
Dynamic memory is allocated on the heap (heap sort)

The problem of using memory across functions

Structure
Why do you need structs
In order to represent some complex things, ordinary basic types cannot meet our actual requirements
. What is a structure?
Combine some basic type data to form a new composite data type, which is called a structure.
How to define a structure
3 ways
//The first
struct Student// defines a data type
{
int age;
float score;
char sex;
};

//The second
struct Student2
{
int age;
float score;
char sex;
} st2;


//The third This kind of
struct// defines a data type
{
int age;
float score;
char sex;
} st3;

how to use structure variables
Assignment and initialization
The whole value can be assigned and stored at the same time of definition.
If the definition is played, it can only be assigned individually How to get the value

of each member in the
structure variable 1. Structure variable name. Member name
2. Pointer variable name -> member name (the second is more commonly used)
Pointer variable name -> member name will be converted to (*pointer variable name).member name in the computer.
So
these two methods are equivalent

examples:
struct Student//Defines a data type
{
int age;
float score;
char sex;
};

int main(void)
{
struct Student st={80,66.6,'F'};//Define a variable

struct Student * pst = &st;//Define a pointer variable, The type is struct Student
//&st cannot be changed to st
st.age; //The first way
pst->age;//The second way
}

1. pst->age will be converted to (*pst) inside the computer .age, no why
This is the meaning of ->, this is a hard rule

2. So pst->age is priced at (*pst).age is also equivalent to st.age

3. The reason why we know pst-> age is equivalent to st.age because pst->age is converted to
(*pst).age to execute

4.pst->The meaning
of age: The age member in the structure variable pointed to by


pst The problem of passing structure variables and structure variable pointers as function parameters
It is recommended to use a structure pointer variable as a function parameter to pass the
structure variable operation
. Structure variables cannot be added, subtracted, or multiplied and divided by each other,
but structure variables can be assigned to each other
. Example:
struct Studnet
{
int age;
char sex;
char name[100];
};//Semicolons cannot be omitted
struct Studnet st1,st2;
st1+st2 st1*st2 st1/st2 are both wrong,
st1 = st2 or st2 = st1 are both correct
Example
: Dynamically construct to store student information The structure array of .
Dynamically constructs a number, stores the information of the students,
and then sorts the output according to the score.

Enumeration
What is an enumeration
List all possible values ​​of a thing
How to use an enumeration

Advantages and disadvantages of enumeration The
code is safer to
write Trouble

Bitwise
operators: &---in place of
&& Logical AND is also called and
&& is completely different from
& 1&1 = 1
1&0 = 0
0&1 = 0
0&0 = 0
5&7 = 5
21&7 = 5;

|---Bitwise OR

|| Logical OR
| Bitwise OR

1 | 0 = 1
1 | 1 = 1
0 | 1 = 1
0 | 0 = 0

~--- Bitwise inversion
~ i is Invert all the binary bits of the i variable

^ bitwise exclusive OR
Same as 0
, different as 1
1 ^ 0 = 1
0 ^ 1 = 1
1 ^ 1 = 0
0 ^ 0 = 0

<<--bitwise left shift
i< <3 means to shift all the binary bits of i to the left by 3 bits, and fill the right with zeros.
Left shift by n bits is equivalent to multiplying by 2 to the nth power
. Interview questions:
A)i = i*8;
B)i = i << 3;
Which statement above executes faster?
Answer: B

>>--bitwise right shift
i>>3 means that all the binary bits of i are shifted to the right by 3 bits, and the left is generally filled with zeros. Of course, it is also possible to add 1
to the left to shift n bits, which is equivalent to dividing by 2 to the power of n , the premise is that the data cannot be
lost Interview questions:
A)i = i/8;
B)i = i >> 3;
Which statement above is faster to execute?
Answer:

The practical significance of the B-bit operator
Through the bit operator, we can operate the data accurately to each bit


>>--bit right shift



Topic
: original code
, also called symbol-absolute value code,
the highest bit 0 means positive 1 means negative, and the rest of the binary is the number The binary bit of the absolute value The

original code is simple and easy to understand Addition and
subtraction operation assignment
There are four operations of addition, subtraction, multiplication and division, which increases the complexity of the
CPU . Application Code- shift Code-shift means that the value is shifted by n bits, n is called the code-shift amount Code-shift is mainly used for the storage of the order code of floating-point numbers Complement code Convert known decimal to binary Calculate positive integer to binary to divide by 2 and take the remainder until the quotient is zero , the remainder is sorted in reverse order to convert the negative integer to binary first find the binary code of the positive integer corresponding to the negative number, then invert all the bits, add 1 to the end, if there are not enough digits, add one to the left (according to the size of the container defined by yourself) , such as int 32 bytes eg:-3's complement 1111....1101 (32 bits in total) FFFFFFFD



















) To

find zero to binary
, all zeros

are known to be converted from binary to decimal
. If the first digit is 0, it indicates a positive integer, and it is calculated according to the ordinary method. If the first digit

is 1, it indicates that it is a negative integer .
It is the absolute value of the negative number
(eg:

).

If all zeros are used, the corresponding decimal number is zero


. Learning
objective: What is the range of numbers that can be stored in a variable of type int in vc++6.0?
The largest integer that can be stored by an int type variable is expressed in hexadecimal 7FFFFFFFF
The absolute value of the largest negative integer that can be stored by an int type variable is expressed in hexadecimal as 80000000. For
details, see the complement code

. The binary code of the smallest negative number is How many?
What is the binary code of the largest positive number?
Knowing the binary code of an integer to find the original number? What happens if the
number exceeds the maximum positive number?
Overflow, only the bytes of the position will be intercepted Mutual conversion of
different data

Hexadecimal conversion

String processing


linked list:

pointer array and array pointer
Pointer array, it is an array, each element is a pointer
array pointer, it is a pointer, points to an array pointer


*/


 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324820924&siteId=291194637