zoukankan      html  css  js  c++  java
  • Array and its point.

    a is the array name.

    &a is the ponit of 2-D array which contains a[5].

    the type of &a should be int (*)[5]. oh what is int(*)5................................

    int(*p1)[5] = (int(*)[5])a; p1++;

    int *p2 = (int *)(&a + 1);

    int (*p3)[5]=&a; a++;

    p1==p2==p3

    int **p p is a point for 2D-array.

    int *p[5] p is a point for 1D-array. The array contains 5 points.

    int (*p)[5] is a point for 1D-array. The array contains any points. any points of it contain 5 int number.

      int a[5]. a is a 1-D array of 5 numbers. int *p=a. p is the point of a. so, the size of p should be 20. But actually, the size of p is just 4.  You must use the form of int(*p)[5].

     四、用法:
           (1)、int **Ptr 
           因为是指针的指针,需要两次内存分配才能使用其最终内容。首
    先,Ptr = ( int ** )new int *[ 5 ];这样分配好了以后,它和(2)的
    意义相同了;然后要分别对 5 个指针进行内存分配,例如:
      Ptr[ 0 ] = new int[ 20 ];
      它表示为第 0 个指针分配 20 个整数,分配好以后, Ptr[ 0 ] 为指
    向 20 个整数的数组。这时可以使用下标用法 Ptr[ 0 ][ 0 ] 到
    Ptr[ 0 ][ 19 ] 了。
          如果没有第一次内存分配,该 Ptr 是个"野"指针,是不能使用
    的,如果没有第二次内存分配,则 Ptr[ 0 ] 等也是个"野"指针,也
    是不能用的。当然,用它指向某个已经定义的地址则是允许的,那是另外
    的用法(类似于"借鸡生蛋"的做法),这里不作讨论(下同)。
          (2)、int *Ptr[ 5 ] 
          这样定义的话,编译器已经为它分配了 5 个指针的空间,这相当
    于(1)中的第一次内存分配。根据对(1)的讨论可知,显然要对其进行一次
    内存分配的。否则就是"野"指针。
          (3)、int ( *Ptr )[ 5 ]
          这种定义我觉得很费解,不是不懂,而是觉得理解起来特别吃力,
    也许是我不太习惯这样的定义吧。怎么描述它呢?它的意义是"一群"
    指针,每个指针都是指向一个 5 个整数的数组。如果想分配 k 个指针,
    这样写: Ptr = ( int ( * )[ 5 ] ) new int[ sizeof( int ) * 5 * k ]。
    这是一次性的内存分配。分配好以后,Ptr 指向一片连续的地址空间,
    其中 Ptr[ 0 ] 指向第 0 个 5 个整数数组的首地址,Ptr[ 1 ] 指向第
    1 个 5 个整数数组的首地址。
       综上所述,我觉得可以这样理解它们:
       int ** Ptr <==> int Ptr[ x ][ y ];
       int *Ptr[ 5 ] <==> int Ptr[ 5 ][ x ];
       int ( *Ptr )[ 5 ] <==> int Ptr[ x ][ 5 ];
       这里 x 和 y 是表示若干的意思。

  • 相关阅读:
    js 去掉文本域中的空格
    网站开发步骤
    获取客户端、服务器、本地IP地址
    c#用反射原理递归遍历复杂实体对象
    jquery1.8在ie8下not无效?
    状态模式
    虚函数和抽象函数的区别
    HashTable、HashSet和Dictionary的区别
    sql视图学习笔记--视图
    html背景为灰色 不能操作,中间div可以操作
  • 原文地址:https://www.cnblogs.com/gaoxianzhi/p/4439466.html
Copyright © 2011-2022 走看看