C++ 数组指针问题
天蓬老师
天蓬老师 2017-04-17 12:06:07
0
5
746
#include<iostream.h>
void main(){
    int n[][3]={10,20,30,40,50,60};
    int (*p)[3];
    p=n;
    cout<<p[0][0]<<","<<*(p[0]+1)<<","<<(*p)[2]<<endl;
}

跪求解答,这么声明的话p是指向一个有三个元素的数组吗?

天蓬老师
天蓬老师

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

reply all(5)
黄舟

Brother, what’s wrong with you haha
It’s been so long since I’ve touched such a basic thing
I recommend you a book on 495 C language problems you must know
Let’s talk about your problem
int p in (*p)[3] is a pointer to an array. This array has 3 elements of type int
, so p and n can wait

黄舟

The type of p should be int[3]~

伊谢尔伦

The p in

int(*p)[3] is a pointer to an array. The one-dimensional space of this array is uncertain, and the two-dimensional space has three elements. All elements are of type int, so n is used for assignment. For p.
In C language, *p is equivalent to an array of uncertain length.
So int(*p)[3] is equivalent to int p[][3]

左手右手慢动作

p seems to be the first address of an int array of unlimited length, and the first 6 are 10 to 60, while the following are random garbled characters.

PHPzhong

Pfft, did you learn from Tan Xqiang?
C++ main function declaration has never been written in this way:

void main()
Only

int main()
and

int main(int argc, char *argv[])
Then, regarding the reading of variable declaration, starting from the variable name, from inside (brackets) to outside, from right to left:


int (*p)[3]p is a
pointer to
array of 3 elements of
int

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