search

Home  >  Q&A  >  body text

java - c/c++ 指针指向 数组和数据类型 判断时会不会有二义性?

int dd=1;
int *a=ⅆ    // 1
int *c=new int[2];  //2

这里1说明a是int 类型的指针,2说明c是int数组类型的指针.对不?

这段数据结构代码

两个理解:
1.rowlist是个动态数组指向一大块内存,数组里的元素存储着指向LinkList<int>链表的地址。即rowlist是指针数组。如图一

2.rowlist指向一个内存,这个内存存储着<LinkList< int > * >类型的指针,即rowlist是指针的指针。如图二

感觉两个都可以,是要看具体实现吗。比如rowlist=new ...是采用第一种?

天蓬老师天蓬老师2796 days ago791

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:55:58

    int dd=1;
    int *a=&dd;    // 1
    int *c=new int[2];  //2

    The subject said:

    Here 1 indicates that a is a pointer of type int, and 2 indicates that c is a pointer of type int array. Right?

    That’s not the case, a and c are actually the same thing, they are pointers to int. c is not a "pointer of type int array".

    Why is c originally an int pointer, but it can actually point to an int array? Because when the int array is assigned to the int pointer, the C language secretly converts it (the int array) into the first element of the int array. The professional term for this "sneak conversion" is called "implicit conversion".

    So to summarize:

    1. The variable c is not a pointer of type int array, but a pointer of type int;

    2. Then why variable c can point to an int array? It’s because there is an implicit conversion;

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:55:58

    int *c=new int[2];  //2

    c is a pointer to int. Pointer to array should be written as

    int (*c)[2]=new int[2];

    How many bytes will the pointer +1 move? Give it a try.

    reply
    0
  • Cancelreply