Why am I being told that an array is a pointer? What is the relationship between arrays and pointers in C++?

后端 未结 6 1054
情书的邮戳
情书的邮戳 2020-12-02 02:39

My background is C++ and I\'m currently about to start developing in C# so am doing some research. However, in the process I came across something that raised a question abo

6条回答
  •  心在旅途
    2020-12-02 03:02

    Besides what has already been told, there is one big difference: pointers are variables to store memory addresses, and they can be incremented or decremented and the values they store can change (they can point to any other memory location). That's not the same for arrays; once they are allocated, you can't change the memory region they reference, e.g. you cannot assign other values to them:

    int my_array[10];
    int x = 2;
    
    my_array = &x;
    my_array++;
    

    Whereas you can do the same with a pointer:

    int *p = array;
    p++;
    p = &x;
    

提交回复
热议问题