Home  >  Q&A  >  body text

C++ 数组指针问题

#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是指向一个有三个元素的数组吗?

天蓬老师天蓬老师2715 days ago694

reply all(5)I'll reply

  • 黄舟

    黄舟2017-04-17 12:08:07

    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

    reply
    0
  • 黄舟

    黄舟2017-04-17 12:08:07

    The type of p should be int[3]~

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 12:08:07

    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]

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 12:08:07

    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.

    reply
    0
  • PHPz

    PHPz2017-04-17 12:08:07

    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

    reply
    0
  • Cancelreply