zoukankan      html  css  js  c++  java
  • 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.




  • 相关阅读:
    Openshift与Kubernetes的区别
    chrome显示正在等待可用的套接字如何解决
    wordpress上传图片附件时把绝对地址修改成相对地址
    flashfxp传输代码变形如何解决
    wordpress禁用模板编辑功能
    nginx设置Expires启用浏览器缓存Leverage browser caching
    如何设置ExpiresDefault启用浏览器缓存Leverage browser caching
    国外常用社交分享代码(纯代码无需插件)
    python识别网站所用技术
    nginx设置http 301重定向到https
  • 原文地址:https://www.cnblogs.com/pangblog/p/3257913.html
Copyright © 2011-2022 走看看