C Programming Language Notes (v) Pointers and arrays

Source: Internet
Author: User

5.1 Pointers and addresses
The pointer is a variable that holds the address of the variable ANSI C uses the type void* (pointer to void) instead of char * as a generic pointer to the type unary operator & can be used to fetch an object's address: p = &c assigns the address of C to the variable p, We call p a pointer to C. The address operator & can only be applied to objects in memory, that is, variables and array elements. Cannot act on an expression constant or register variable unary operator * is an indirection or indirect reference operator. When it acts on the pointer, The object pointed to by the access pointer is also a variable, so it can be used directly in the program without using the indirect reference method.

  

5.2 Pointers and Function parameters
The C language passes parameters to the called function as a value, so the called function cannot directly modify the values of the variables in the key function.

  

5.3 Pointers and arrays
Any operation that can be done with an array subscript can be accomplished by pointers. Generally speaking, a program written with pointers executes faster than a program written in an array to calculate the value of an array element a[i], and the C language actually converts it to * (A+i), then evaluates it, So in the program these two forms are equivalent pa[i] = * (pa+i) An expression that is implemented by an array and subscript can be equivalent to an array name and a pointer by the pointer and offset a difference between. Pointer is a variable when you pass an array name to a function, The address of the first element of the array is actually passed in the called function, which is a local variable, so the array name must be a pointer, which is a variable that stores the address value.

  

5.4 Address arithmetic operations
If P is a pointer to an element in the array, then p++ will perform a self-increment operation on p and point to the next element, and P+=i will increment the p by the addition of I, so that it points to the elements of the I element after P's current point and the integer cannot be converted from one to the other. But 0 is the only exception to the constant 0 can be assigned to the pointer, the pointer can also be compared with the constant 0 is often used with the symbolic constant null instead of the constant 0, so as to make it clear that the constant 0 is the pointer of a special value of the pointer subtraction is also meaningful: if P and Q point to an element in the same array, and Then q-p+1 is the only number of pointers between P and Q points The arithmetic operation of the pointer is consistent: if the data type being processed is a floating-point type that occupies more storage space than the character type, and P is a pointer to a floating-point type, then p++, P The valid pointer operation to the address of the next floating-point number includes the assignment operation between pointers of the same type, and the addition or subtraction of pointers to integers points to the subtraction or comparison operation between two pointers to elements in the same array; Assigning a pointer to a value of 0 or a comparison between a pointer and 0 other so the form of the pointer operation is illegal

  

5.5 Character pointers and functions
A string constant is a character array in the internal representation of a string, and the character array ends with a null character ' s ', so the program can find the end of a character array by checking for null characters. The storage unit occupied by string constants is therefore 1 larger than the number of characters in double quotes

  

5.6 pointer arrays and pointers to pointers
Pointers themselves are variables, so they can also be stored in arrays like other variables

  

5.7 Multi-dimensional arrays
The C language provides multidimensional arrays similar to matrices, but they are not as widely used as pointer arrays

  

5.8 Initialization of the pointer array 5.9 pointers to multidimensional arrays
For beginners, it is easy to confuse the difference between a two-dimensional array and a pointer array
int a[10][20];
int *b[10];
Syntactically, a[3][4] and b[3][4] are legal references to an int object

But A is a true two-dimensional array that allocates 200 of the length of the int type storage space

For B, the definition allocates only 10 pointers and does not initialize them, and their initialization must be done in a way that is displayed, such as static initialization or initialization by code.

Another advantage of an array of pointers is that each line of the array can be of different lengths

The most frequent use of pointer arrays is to hold strings of different lengths

  

5.10 Command-line arguments
In an environment that supports C, you can pass command-line arguments to a program when the program starts executing. When the main function is called, Main, it takes two arguments. The first argument is not the number of run-time command-line arguments (ARGC) The second argument is a pointer to an array of strings, where each string corresponds to a parameter

C language Programs in UNIX systems have a common convention: Arguments that begin with a minus sign represent an optional flag or parameter.
A line of text that does not match the pattern is assumed to be printed with-X so that the line number is printed with-n


  

5.11 Pointers to functions
In C, the function itself is not a variable, but a pointer to a function can be defined as a pointer of this type that can be assigned to the array, passed to the function, and as the return value of the function//pointer to the function int (*comp) (void *,void *); *comp represents a function

5.12 Complex declarations
C-language declarations cannot be read from left to right, and use too many parentheses int *f ();//f is a function that returns a pointer to int type int (*PF) (); PF is a pointer to a function that returns an object of type int indicating a difference in meaning between them, * is a prefix operator whose precedence is lower than (), so parentheses must be used in the declaration to ensure proper binding order

  

C Programming Language Notes (v) Pointers and arrays

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.