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.




  • 相关阅读:
    Global 文件中挂接HttpModule事件的方法列表参考
    百行代码打造一个DI容器(支持瞬时生命周期、单利生命周期、构造函数自动注入、属性自动注入、字段自动注入)
    松耦合服务调用利器服务分发器
    架构视角面面观之: WebPage能支持DI注入那该多好
    架构视角面面观之: WebPage能像MVC的ViewPage那样支持泛型节约不少代码量的?
    安装 Nuget 插件过程以及注意事项
    给Web Api 再加把柴让它更火起来
    Mini 容器泛型类型的使用
    JAVA虚拟机08垃圾回收HotSpot的算法实现细节
    Web UI 设计(网页设计)命名规范
  • 原文地址:https://www.cnblogs.com/pangblog/p/3257913.html
Copyright © 2011-2022 走看看