java - c/c++ 指针指向 数组和数据类型 判断时会不会有二义性?
天蓬老师
天蓬老师 2017-04-18 10:53:58
0
2
778
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 ...是采用第一种?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(2)
伊谢尔伦
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;

洪涛
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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template