Intermediate C Language

Source: Internet
Author: User

The following is a C language note from a senior student ....

Some of them are from other materials. I hope it will be useful to you.

1. expression value: the expression has the value of the expression. It is unknown and short-lived.
2. Case constant (cannot be an operation)
3. Data zone (stack, static zone, heap) and Code Zone
4. sizeof and array (the following description does not consider machines, platforms, and other factors ).
I.
Int I [10];
Int * P
P = I;
The above sizeof (I) is 40. But sizeof (P) is 4. cause: the array name is not a pointer.

II.
Array size:
Sizeof (I)/sizeof (I [0]);
The former is 40, and the latter is 4, so there are 10.
III.
If you pass the array name as a parameter to the function, the parameter of the function must be a pointer. therefore, the array size cannot be calculated in the function. because sizeof (this parameter) is 4. so I think a lot of functions need to pass in size. instead of calculating the size in the function.
5. Const

Const int;
Int const;
Const int *;
Int * const;
Int const * a const;
The first two functions are the same: A is a constant integer. Third, it means that a is a pointer to a constant INTEGER (that is, an integer cannot be modified, but a pointer can be ). Fourth, a is a constant pointer to an integer (that is, the integer to which the Pointer Points can be modified, but the pointer cannot be modified ). The last one means that A is a constant pointer to the constant integer number (that is, the pointer and the integer number pointed to cannot be modified ).

6. typedef traps:

Typedef char * pstr;
/* If we use this method :*/
Int mystrcmp (const pstr, const pstr );
/* What we want to express is :*/
Int mystrcmp (const char *, const char *);
/* (Two char pointers pointing to constants )*/
/* It can be interpreted:
Int mystrcmp (char * const, char * const );
/* (Two regular pointers pointing to Char )*/

Analysis:
Const modifies pstr, while pstr is defined as char * instead of char.

To:
Typedef const char * cpstr;
Int myctrcmp (cpstr, cpstr );

7. Complex pointer Parsing
For example, INT (* func) [5] [6]) [7] [8];
Func is a pointer to a three-dimensional array. The large elements of this array are two-dimensional arrays with 5x6 int elements, and the pointer to a three-dimensional array is another element of a three-dimensional pointer array.

8. Why is there an anonymous struct?
To prevent others from declaring variables of this type.
9. struct size
Struct {
Char ch;
Int I;
Float F;
Char CH2;
};
Struct {
Char ch;
Char CH2;
Float F;
Int I;
} B;

Sizeof (A) = 16
Sizeof (B) = 12
Why are the members in the same order but in different order occupying different spaces?
Storage of A: CH, _ (supplemented with three bytes), I, float F, CH2 ,_,_,_
B Storage: CH, CH2, _, _ F, I
It is concluded that the type of the largest byte in the member is used as the unit, and then the variable is filled. if this parameter is set, enter the unit. If this parameter is not set, enter the unit. for example, store CH and CH2 of B in four-byte memory, and leave two locations empty.
Conclusion: Pay attention to the sequence of the members when declaring struct.
10. malloc and calloc
I. What is better for malloc (size) and calloc (number, size of each?
Use calloc.
Cause: although the size can be calculated by the number *, what should I do if I want to make this value very large? Therefore, separate writing (calloc) is better than calculating this size (malloc). This is because I personally think. But the calloc conclusion should not be personally considered.
Ii. malloc (0)
Although a memory of 0 is applied for, the result returned by malloc is not equal to null, that is, it has returned memory. How big is it? Forgot...

11. File pointer
I.
When a file pointer is used, the pointer refers to a struct, which not only contains a variety of information related to the file, but also points to a buffer, the buffer and file have another stream that is sometimes read and written.
That is to say, fopen not only returns a file pointer, but builds the entire file reading environment.
Ii. File text and binary.
For example, in text format, the ASCII value of each word is '49', '50', and '51'
In binary mode, a byte can represent 1111011.

Use Cases: convenience for humans and convenience for computers

In addition, a link http://www.blabla.cn/ref/ascii.html with an ASCII encoding table

12. Preprocessing
I.
Do not ignore spaces in macro definitions:
# Define f (x) + 1 // actually defines F as (x) + 1

For macros without parameters, brackets must be used if there are more than one macro value.
# Define max (m + n)
Brackets should be added for each parameter; otherwise, the calculation priority may be affected.
# Define ABS (x) (x> = 0 )? X:-x
Z = ABS (a + B);/* equivalent to Z = (a + B> = 0 )? A + B:-A + B
// After modification:
# Define ABS (x)> = 0? (X):-(x ))
Use typedef instead of macro definition to define the type.

II.
You can use both typedef and macro to define a new type. Which is better.
A: typedef is better.
Cause: int * a, B; A and B are of the type, A is the pointer, and B is the integer. similarly, when using the new type defined by a macro, the new type pointer or type is unknown.

III.
Because header files can be nested, the C file may contain the same header file multiple times, which may cause repeated definitions.
Enable Conditional compilation to avoid repeated inclusion
For example
# Ifndef _ headerfilexxx __
# DEFINE _ headerfilexxx __
...
File Content
...
# Endif
4.
# Include "XXXX. h"
Go to the current directory and find the system's default directory.
# Include <XXXX. h>
To the default system directory.

13. Common Database functions (Summary: Take assert as an example)
Diagnostic functions (assert. h)
Test A condition. If the result is false, the program is terminated. If the result is true, no response is made.

Do not use the assert function in the execution logic of the program. It is only used for debugging.
That is, the IF (condition) Statement of the program cannot be replaced by assert (condition.

If the code is added at the beginning of the program:
# Define ndebug
All the assert functions in this program are invalid and do not need to be deleted one by one. (To be verified. Because I tried it, why didn't I use it)

14. What is the role of the keyword static?
In the C language, the keyword static has three obvious functions:
In the function body, a variable declared as static remains unchanged when the function is called.
In a module (but in the external body of a function), a variable declared as static can be accessed by the function used in the module, but cannot be accessed by other functions outside the module. It is a local global variable.
In a module, a function declared as static can only be called by other functions in this module. That is, this function is restricted to use within the local scope of the module that declares it.

15. The pointer can be used as follows:
# Include "stdio. H"
Void main ()
{
Int A = 1234;
// 0x0012ff7c is the value of &.
Int * P = (int *) 0x0012ff7c );
Printf ("& A = % x/N", & );
Printf ("A = % d/N * P = % d/N", A, * P );
}

16. Finally, leave a question.
If you do not apply for a new space, sort the array in reverse order.

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.