Learning notes about "C and pointers" _c language

Source: Internet
Author: User
Tags function definition function prototype strlen first row

With the previous foundation, this article is simply a record of some previously unnoticed and worthy of learning knowledge.

Chapter I.

The author believes that the use of #if 0 .... #endif比用/* and * * is good, because the latter cannot be nested. But there is no explanation for//.

Chapter II 

Three-letter words, with two question marks plus one symbol for another symbol, comparison is similar to the escape character. Consult some of the data, its use is related to the compiler, to understand, to prevent string constants are incorrectly interpreted.

Copy Code code as follows:

?? (==> [??] < ==> {?? = ==> #

??) ==>]?? > ==>}?? /==> \

??!   ==> | ?? ' ==> ^?? -==> ~


For a deeper nested function, the author suggests dividing it into several functions to implement without using the tab indent too much.

Chapter III Data

For complex uses of static, when it is used for variable declarations outside of a function definition or code block, static is used to modify the connection properties of the identifier, from external to internal, but the storage type and scope of the identifier are unaffected. Functions or variables declared in this manner can only be accessed in the source file in which they are declared. When used for variable declarations within a block of code, static is used to modify the storage type of a variable, from an automatic variable to a static variable, but the link properties and scope of the variable are unaffected. Variables declared in this manner are created before the execution of the program and persist throughout the execution of the program, rather than every time the code block is executed and destroyed after the execution of the code block is completed.

Fifth chapter operators and expressions

Shift operations, when the number of digits moved is negative, the exact result is related to the compiler or undefined, such as a<<-5 may be left 27-bit.

The operation efficiency of a+=1 is higher than that of a=a+1, equivalence a[2 * (Y-6*f (x))]=a[2 * (Y-6*f (x))] + 1 is compared with a[2 * (Y-6*f (x)) + + 1, the latter does not have to compute the subscript repeatedly.

The form of sizeof X is allowed. sizeof () does not evaluate an expression, so the A in sizeof (A=B+1) is not assigned a value.

Use only-> when accessing a member of a pointer to a struct.

Sixth chapter Pointers

An uninitialized pointer can cause an error. int *a; *a = 12, which causes the contents of the address that a points to be modified, and the result is unpredictable.

The author thinks that the return value of a null pointer when it is not found, such as a search element, is a common technique for C, but it violates the principles of software engineering: "It is dangerous to represent two different meanings with a single value, because it is easy to find out which is the true intention of the future." A secure policy is to return two values indicating whether the status value was successful and the element value found when the lookup was successful.

Seventh Chapter function

A function prototype declaration without parameters should write this way: int func (void); The intent is not to confuse the old style declaration.

Recursion solves the problem more clearly than the recursion, for a complex problem, it is difficult to use the iterative form implementation, the simplicity of recursive implementation can compensate for its cost. Fibonacci is a common example of recursion, but there are many redundant computations, too much overhead, which are actually not as iterative implementations.

Copy Code code as follows:

Iterative implementation of Fibonacci

Long Fibonacci (int n)
{
Long result;
Long Previous_result;
Long Next_older_result;

result = Previous_result = 1;

while (n > 2) {
n-= 1;
Next_older_result = Previous_result;
Previous_result = result;
result = Previous_result + Next_older_result;
}
return result;
}


Use of the variable parameter list: header file Stdarg.h, which declares a type va_list and three macros Va_start, Va_arg, Va_end. Access the value of a parameter by declaring a variable of type va_list with these macros. The function declares that a Var_arg variable is used to access the indeterminate part of the argument list, which is initialized by Va_start. The 1th parameter is the name of the va_list variable, and the 2nd parameter is the last named argument before the ellipsis. The initialization process points the Var_arg variable to the 1th parameter of the variable parameter section. Va_arg accepts two parameters: Va_list and the type of the next argument in the argument list. Va_arg returns the value of the parameter and points the Var_arg to the next variable parameter. Call Va_end when the access is complete.
Copy Code code as follows:

#include <stdarg.h>

float average (int n_values, ...)
{
Va_list Var_arg;
int count;
float sum = 0;

/* Ready to access variable parameters * *
Va_start (Var_arg, n_values);

/* Add a value taken from the variable parameter list * *
for (count = 0; count < n_values; count + = 1) {
Sum + + va_arg (var_arg, int);
}

/* Complete processing of variable parameters * *
Va_end (VAR_ARG);
return sum/n_values;
}


A macro with a variable parameter cannot determine the number of parameters and the type of parameter, which may cause a rise in the default parameter type. The solution to these two problems is to use named parameters, which is the reason why there is always a named parameter in the variable argument list.

Eighth Chapter Array

int Array[10];int *ap =array+2; After that, ap[0] is legal in C, which is equivalent to array[2],ap[-1] is also legal, that is array[1.

Pointers are more efficient than arrays: the ap++ of A For loop is more efficient than the array[a in the loop body] = 0, and the former is multiplied only once, used for 1 to multiply the length of the data type, and the latter needs to be calculated each time.

Copy Code code as follows:

/* Use array/*
int array[10], A;
for (a = 0; a< a +=1)
Array[a] = 0;

/* Use pointers/*
int array[10], *ap;
for (AP = array; ap< Array + ap + +)
*ap = 0;


The initialization time for arrays, especially large arrays, can be significant, so when the initialization of an array is local to a function or code block, it is worth considering whether the program will reinitialize it every time. If not, declare the array as static.

Using pointers to access multidimensional array methods, such as for array int matrix[3][10], declaring int *mp = matrix is wrong because the matrix is not a pointer to an integer, but a pointer to an integer array. int (*p) [Ten] = matrix is OK, p points to the first row of the matrix to achieve row-by-line access to the array. If you need to access it individually, use int *pi = &matrix[0][0] or int *pi = matrix[0 to point to the first element. the int (*p) [] = matrix; is incorrect, and its value is adjusted according to the length of the empty array, which the compiler cannot capture. The function pass parameter is similar.

Multidimensional arrays are explicitly initialized, and only the first dimension can be extrapolated, and the other dimensions cannot be omitted.

Nineth chapter strings, characters, and bytes

Cautious use of unsigned numbers: strlen returns unsigned numbers, so if (strlen (x)-strlen (y) >=0) ... It's always true. In this case, it should be written as if (strlen (x) >=strlen (y)) ... or convert it to int by force type conversion.

Strtok saves the local state information of the function it processes, so you cannot use it to parse two strings at once.

A string function encounters a null byte end operation, and when you want to work with non string data, you can use another set of related functions: memcpy, Memmove, memcmp, MEMCHR, memset.

Chapter Tenth structure and union

parameter is a struct function, passing the pointer is more efficient than passing a value call, because the latter needs to create a copy of the structure. F (type_struct *s) {s->x}; call is F (&s). It is more efficient to declare a register variable if the member is accessed more than 3 times for this struct. To avoid improper modification, you can declare the parameter as const and assign the return value to the original structure (or one of its members).

The bit segment is simply understood, which is a special structure that specifies the length of the member.

13th Chapter Advanced Pointer Topic

The use of callback functions can solve problems similar to those of unknown types of data, and this is the first time that a callback function is systematically known.

14th Chapter Preprocessor

Eliminates the multiple-contained risk of a method that writes the following in each header file:

Copy Code code as follows:

#ifndef _headrname_h
#define _HEADRNAME_H 1
* * All the stuff, want in the header file*/
#endif

The first time it is included, the _headrname_h is defined as 1 and is ignored when it is included again. Even if you write it as a #define _HEADRNAME_H, you can do it. However, you should still avoid multiple inclusions as much as possible.

15th Chapter input \ Output function

Freopen is used to open (or reopen) a particular file stream, the prototype: File *freopen (char const *filename,char const *mode, file *stream), where the last parameter is the stream that needs to be opened. It first attempts to close the stream and then reopen the stream with the specified file and mode, failing to return null and successfully returning the third argument.

Ungetc the previously read character back into the stream so that it can be reread later. "C programming Language" has a character processing example to use it, here to review. When Fseek, Fsetpos, or rewind change the position of the stream, all returned characters are discarded.

The difference between gets and puts and fgets and fputs is that when gets reads a line of input, a newline character that is not at the end of the buffer store, puts writes a string, adds a newline character to the output after the string is written. In addition, gets does not judge the length of the buffer, which poses a danger.

Feof determines whether the stream is at the end of the file, ferror the error status of the report stream, and clearerr the error flag for the specified stream.

Tmpfile creates a temporary file to save the data and is deleted at the end of the program. The name of the temporary file is created by Tmpnam.

16th Chapter Standard Function Library

Volatile is a type modifier that is designed to modify variables that are accessed and modified by different threads, preventing the compiler from "Optimizing" a program in a way that might modify the meaning of the program.

vprintf, vfprintf, vsprintf are used to print a variable parameter list that functions like a corresponding PRINFT function, but the parameter is a variable argument list arg.

Getenv gets the environment variable and returns NULL if it is found with a pointer.

Locale is a set of specific parameters that each country may be different in order to enhance the universality of C in the world, without detailed description.

For the 17 classic abstract data types and the 18th chapter of the runtime environment, the former is more familiar with the combination of the latter and the Assembly, but only a cursory glance, this book is read.

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.