c pointer and array

Pointer:  A pointer is a variable that contains the address of a variable.

  if c is a char and p is a pointer that points to it, we could represent the situation this way:


    &:The unary operator & gives the address of an object, so the statement

                                        p = &c;

    assigns the address of c to the varible p, and p is said to "point to " c.The & operator only applies to objects in memory:


                                           array vs pointer

         In C, there is a strong relationship between pointers and arrays, strong enough that pointers and arrays should be discussed simultaneously. Any operation that can be achieved by array subscripting can also be done with pointers. The pointer version will in general be faster but, at least to the uninitiated, somewhat harder to understand. 

          The correspondence between indexing and pointer arithmetic is very close. By definition, the value of a variable or expression of type array is the address of element zero  of the array. Thus after the assignment 
                                      pa = &a[0];
 pa and a have identical values. Since the name of an array is a synonym for the location of the initial element, the assignment pa=&a[0]can also be written as 
                                        pa = a;

        Rather more surprising, at first sight, is the fact that a reference to a[i]can also be written as *(a+i). In evaluating a[i], C converts it to *(a+i)immediately; the two forms are equivalent. Applying the operator &to both parts of this equivalence, it follows that &a[i] and a+i are also identical: a+i is the address of the i-th element beyond a. As the other side of this coin, if pais a pointer, expressions might use it with a subscript; pa[i]is identical to *(pa+i). In short, an array-and-index expression is equivalent to one written as a pointer and offset. 
        There is one difference between an array name and a pointer that must be kept in mind. A pointer is a variable, so pa=a and pa++ are legal. But an array name is not a variable; constructions like a=pa and a++ are illegal. 


       Pointers and integers are not interchangeable.Zero is the sole exception: the constant zero may be assigned to a pointer, and a pointer may be compared with the constant zero. The symbolic constant NULL is often used in place of zero, as a mnemonic to indicate more clearly that this is a special value for a pointer.NULL is defined in <stdio.h>. We will use NULL henceforth.


       Command-line Arguments

   In environments that supports C, there is a way to pass command-line arguments or parameters to a program when it begins executing. When main is called, it is called with two aguments. The first(conventionally called argc,  for argument count) is the number of command-line arguments the program was invoked with; the second(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.

     The simplest illustration is the program echo, which echoes its command-line arguments on a
single line, separated by blanks. That is, the command

                  echo hello, world
prints the output
                          hello, world 
               By convention, argv[0] is the name by which the program was invoked, so argc is at least 1. If argc is 1, there are no command-line arguments after the program name. In the example above, argc is 3, and argv[0], argv[1], and argv[2] are "echo", "hello,", and "world" respectively. The first optional argument is argv[1] and the last is argv[argc-1]; additionally, the standard requires that argv[argc] be a null pointer.




原文地址:https://www.cnblogs.com/pangblog/p/3257913.html