C语言重温-1-指针
Some tips of Pointers, revisit a classic book The c programming language, written by Kernighan and Ritchie.
1.Pointers and Addresses
The unary operator ‘&’ gives the address of an object, so the statement p = &c;assigns the address of c to the variable p, and p is said to point to c.
 The ‘&’ operator only applies to objects in memory: variables and array elements. It cannot be applied to expressions, constants, or register variables.
The unary operator * is the indirection or dereferencing operator; when applied to a pointer, it accesses the object the pointer points to.
Suppose that x and y are integers and ip is a pointer to int.
int x = 1, y = 2, z[10];| int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ y = *ip; /* y is now 1 */ *ip = 0; /* x is now 0 */ ip = &z[0]; /* ip now points to z[0] */2.Pointers and Function Arguments
Because of call by value, swap can’t affect the arguments a and b in the routine that called it. The function above swaps copies of a and b.
void swap(int x, int y) /* WRONG */{int temp;temp = x;x = y;y = temp;}Pointer arguments enable a function to access and change objects in the function that called it.
void swap(int *px, int *py) /* interchange *px and *py */ { int temp; temp = *px; *px = *py; *py = temp; }3.Pointers and Arrays
The declaration int a[10];defines an array of size 10, that is, a block of 10 consecutive objects named a[0], a[1], …, a[9]. The notation a[i] refers to the i-th element of the array.
If pa is a pointer to an integer,declared as int *pa;, then the assignment pa = &a[0];sets pa to point to element zero of a; that is, pa contains the address of a[0].
 Now the assignment x = *pa;will copy the contents of a[0] into x.
If pa points to a particular element of an array, then by definition pa+1points to the next element, pa+ipoints i elements after pa, and pa-ipoints i elements before. Thus, if pa points to a[0], *(pa+1)refers to the contents of a[1], pa+iis the address of a[i], and *(pa+i)is the contents of a[i].
 
4.Initialization of Pointer Arrays
Consider the problem of writing a function month_name(n), which returns a pointer to a character string containing the name of the n-th month. This is an ideal application for internal staticarray. month_namecontains a private array of character strings, and returns a pointers to the proper one when called.
char *month_name(int n){static char * name[] = {"Illegle month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};return (n<1 || n>12) ? name[0] : name[n]; }5.Pointers vs. Multi-dimensional Arrays
Newcomers to C are sometimes confused about the difference between a two-dimensional array and array of pointers.
 Given the definitions bellow:
then a[3][4]and b[3][4]are both syntactically legal references to a singleint.
But ais a true two-dimensional array: 200 int-sized locations have been set aside, and the conventional rectangular subscript calculation 20 x row + colis used to find the element a[row, col].
For b, however, the definition only allocates 10 pointers and does not initialize them; initialization must be explicitly, either statically or with code.
 Assuming that each element of bdoes point to a twenty-element array, then there will be 200 ints set aside, plus ten cells fot the pointers.
The important advantage of the pointer array is that the rows of the array may be of different lengths. That is , each element of b need not point to a twenty-element vector; some may point to two elements, some to fifty, and some to none at all.
In one word: Compare the declaration and picture fo an array of pointers with those for a two-dimensional array:
char *name[] = { "Illegal month", "Jan", "Feb", "Mar"}; char aname[][15] = { "Illegal month", "Jan", "Feb", "Mar"};6.Pointers and Command-line Arguments
In environments that support C, there is a way to pass command-line arguments or parameters to a program when is begins executing. When mainis called, it is called with two arguments. The first (conventionally called argc, for argument count) is the number of command-line arguments the program was invoked with; the second (conventionally called argv, for argument vector) is a pointer to an array of character strings that contain the arguments, one per string. We customarily use multiple levels of pointers to manipulate these character strings.
 Considering the mainfunction bellow:
Since argvis a pointer to an array of pointers, we can manipulate the pointer rather than index the array. This next variant is based on incrementing argv, which is a pointer to pointer to char, while argcis counted down.
#include <stdio.h> /* echo aommand-line arguments; 2ed version*/ int main( int argc, char *argv[]){while (--argc > 0)printf("%s%s", *++argv, (argc > 1) ? " " :"");printf("\n");return 0; }Since argvis a pointer to the beginning of the array of argument strings, incrementing it by 1 (++argv) makes it point at the original argv[1] instead of argv[0].
總結(jié)
以上是生活随笔為你收集整理的C语言重温-1-指针的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: docker php安装gd扩展_doc
- 下一篇: 魅族android9更新,魅族 17 系
