C Programming Language (2nd edition • New version) Chapter 5th pointers and arrays

Source: Internet
Author: User

pointers: variables that hold variable addresses, which are widely used in C, because 1) pointers are often the only way to express a calculation; 2) using pointers can often write more efficient and compact code; On the other hand, 1) pointers and Goto will cause the program to be difficult to understand ; 2) carelessness can easily lead to a pointer pointing to the wrong place;

Pointers and Arrayis closely related; ANSI C explicitly formulates rules for manipulating pointers, and uses void* instead of char* as Universal PointersThe type of the; 5.1 Pointer and address Organization of Memory: A series of sequential numbered or addressable storage units that can be manipulated individually or in a continuous group; usually 1 bytes can hold a char,2 contiguous byte can hold a short,4 contiguous byte can hold a long; Pointers:A set of storage units capable of storing an address, usually 2 or 4 bytes; for example: int *p; This notation indicates that the *p result is of type int; double atof (char *); The values and parameters of the function are pointers; address operator&:p=&c; Assigning the address of C to a pointer variable p;& can only be used in memory objects (variables and array elements), not in the amount of expressions, constants, or register types; Indirect addressing or indirect reference operators*: Acts on the pointer, accessing the object it points to. such as:px=&x; *px=0; *XP can be used where the value of x changes to 0 where x occurs, for example: *px+=1; equivalent to ++*PX; Equivalent to (*PX) + +; A unary operator follows a right-to-left binding order, so parentheses are required; The pointer can only point to a specific type of object (exception: A pointer to a void type may hold a pointer to any type, but it cannot indirectly refer to itself); 5.2 Pointer and function parameter C pass the value of the parameter to the called function in the form of a pass. So the called function cannot directly modify the value of the variable in the keynote function. so void swap (int x, int y) is wrong, void swap (int *px, int *py) to correctly exchange two elements; Call: Swap (&a, &b); 5.3 pointers and arrays any operations that can be done with an array subscript can be implemented by pointers, and in general the latter executes faster, but increases comprehension difficulty; declares an int a[10]; defines an array. Its 10 elements are stored in an adjacent memory area; pa=&a[0]; is equivalent to pa=a; The array name is the address of the first element of the array, and the reference array element A[i] is equivalent to * (A+i); &a[i] equivalent to A+i, the corresponding pointer pa can also be labeled pa[i], equivalent to * (Pa+i), that is: the array and offset implementation of the expression can be equivalent to the pointer and offset; the array name and pointer have a different: The pointer is a variable and the array name is not, so pa=a; and pa++; yes, and a=pa; and a++; (it) the array name to the function is actually passed its first element address, the parameter is a local variable, so the array name parameter must be a pointer, the pointer in the function to participate in the operation, does not affect the pointer argument in the key function of the value; When defining a function, the parameter char a[], and char *a is equivalent , accustomed to use the latter; The array name is passed to the function, and the function automatically determines whether the pointer or array is processed, and even the array and pointer can be used in the function; the non-first address (for example, the first address of the Subarray) can be passed to the function: F (&a[i]) equivalent to F (a+i); 5.4 Address arithmetic operations a great advantage of C language:The arithmetic operations of pointers, arrays, and addresses are integrated together; Effective pointer arithmeticIncludes four categories: A. Assignment or initialization between pointers of the same type; B. The pointer is added and subtracted from the integer (scaled proportionally to the length of the object pointed to by the pointer); A subtraction or comparison operation between two pointers to elements in the same array ( Exceptions:The pointer arithmetic operation can use the next address of the last element of the array); ( Special CasesYou can assign a pointer to a value of 0 or to a 0 comparison (a symbol constant in a common <stddef.h>, null instead of 0, to indicate that 0 is a special value for a pointer) 5.5-character pointers and functions string Constantsis an array of characters (the inner representation ends with ' *pmessage= '), accessed by a character pointer to its first element; char "Good bye";//defines a pointer with the initial value pointing to a string constant that can be modified, but whose modified string content does not define Char amessage[]= "Good Bye";//defines an array whose individual elements can be modified, but amessage always points to the same address the C language does not provide an operator to process the entire string as a whole; <string.h> contains strcpy , the value of the string processing function *t++, such as strcmp, is the character that the pointer T points to before the self-increment operation, and the suffix + + means that the value of T is changed after reading the character; (it: As mentioned earlier, the unary operator is combined right-to-left, so T combines + + then combine *, i.e. * (t++ The effect of the use of T after the T self-increment, has been verified correct) in and out of the stackStandard usage: *p++=val; The Val is pressed into the stack val=*--p; POPs the top element of the stack into Val 5.6 pointer array and pointer to pointer Char *lineptr[maxlines]; is a Array of pointers(used to store the first character address of the line of text to be sorted), where Lineptr[i] is a character pointer, and Lineptr is an array name, pointing to its first element, *lineptr, which is also a pointer to the first character of a line of text; 5.7 A multi-dimensional array-like matrix. But the use of pointers arrays is not as extensive; two-dimensional arraysis actually a special one-dimensional array, each of its elements is also a one-dimensional array; store char daytab[2][13]={{...},{...}};/by row /define a two-dimensional array, daytab[i][j] can refer to an element if the two-dimensional array is the origin of the function, then the number of array columns must be specified in the function declaration, and the number of rows does not matter: Because a function call passes a pointer, the following three kinds can: a. f (int daytab[2][13]) {...} B.F (int daytab[][13]) {...}//In general, the first dimension of the divisor (subscript) can not specify the size, the remaining dimensions must be explicitly specified in size, c.f (int (*daytab) [13]) {...}//Because the number of rows does not matter, This declaration indicates that the parameter is a pointer to a one-dimensional array with 13 integers, and if the brackets are removed: int *daytab[13]//is equivalent to declaring a one-dimensional array with 13 integer pointer elements; 5.8 Initialization of the pointer array, for example Array of strings: Char *month_name[]={"Illegal Month", "January", "February", ...}; At compile time, the number of initial values is automatically counted and the array length is filled in; 5.9 pointers and multidimensional arrays can easily confuse the difference between a pointer array and a two-dimensional array: int a[10][20]; Allocate 200 bytes of storage space, by 20*i+j to calculate a[i][j] position; int *b[10];  With only 10 pointers assigned and not initialized, each pointer can point to an array, with important advantages: You can point to arrays of different lengths (or not to any objects); The most common use of pointer arrays is to hold strings of different lengths, such as 5.8, 5.10 command-line arguments The C language can pass command-line arguments to the program when the program starts executing, and it has two parameters when calling main: ARGC (for counting, indicating the number of arguments in the command line when the program is run); argv (for parameter vectors, a pointer to an array of strings, one argument for each string) ; These strings are usually processed with multi-level pointers); Conventions:The value of Argv[0] is the program name that starts the program, so ARGC is at least 1, the optional parameter Argv[1]~arf[argc-1];ansi requires ARGV[ARGC] must be a null pointer; (NULL pointer: pointer not pointing to any place or to null (integer 0) Void* is an indeterminate type pointer);//p100-101 not see 5.11 Pointer to function is not a variable by itself, but a pointer to a function can be assigned, stored in an array, passed to a function, returned as a function, and so on; Sort ProgramsUsually consists of 3 parts: Judging the order of two objects Compare OperationsReverse the order of objects. Swap OperationsFor comparison and exchange until sorted correctly. algorithm。   The sorting algorithm is independent of the comparison and exchange operations, so different comparison and exchange functions are called in the sorting algorithm and can be sorted according to different criteria. an int (*comp) (void *, void *) indicates that comp is a pointer to a function, *comp represents a function, and the Calling method: (*comp) (V[i], V[left]) where the parentheses are necessary to ensure that the parts are properly combined; no parentheses: int  *comp (void *, void *) indicates that comp is a function that returns an int type pointer; 5.12 Complex declaration The C language often receives criticism because of the syntax of declarations (especially function pointers); C's syntax tries to make the declaration and use consistent; Because C's declaration cannot be read from left to right and uses too many parentheses, it is confusing when the situation is more complex, such as 5.11 cases; complex declarations are seldom used in practice, and complex declarations can be created by using typedef in simple steps.//p106-109 not seen, The speaking program transforms the declaration into a literal description, and the reverse process;

C Programming Language (2nd edition • New version) Chapter 5th pointers and arrays

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.