Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 数组基址指针_C++_Arrays - Fatal编程技术网

C++ 数组基址指针

C++ 数组基址指针,c++,arrays,C++,Arrays,给定以下代码,ptr和ptr2之间的区别是什么?因为v的大小是20,而&v[0]的大小是4。为什么这会起作用?为什么不像ptr*=&v这样的第三个指针呢。所有这些都有相同的基址,但第一个基址和第二个基址的区别是什么。。。我很困惑。当使用2d阵列时,情况会变得更糟。谢谢你的帮助 #include <iostream> using namespace std; int main(void) { int v[5] = { 1, 2, 3, 4, 5 }; int *ptr

给定以下代码,ptr和ptr2之间的区别是什么?因为v的大小是20,而&v[0]的大小是4。为什么这会起作用?为什么不像ptr*=&v这样的第三个指针呢。所有这些都有相同的基址,但第一个基址和第二个基址的区别是什么。。。我很困惑。当使用2d阵列时,情况会变得更糟。谢谢你的帮助

#include <iostream>
using namespace std;

int main(void)
{
    int v[5] = { 1, 2, 3, 4, 5 };
    int *ptr = v;
    int *ptr2 = &v[0];

    cout << sizeof(v) << endl << sizeof(&v[0]) << endl;
    cout << v << endl << &v << endl << &v[0];
    system("pause");
    return 0;
}
#包括
使用名称空间std;
内部主(空)
{
int v[5]={1,2,3,4,5};
int*ptr=v;
int*ptr2=&v[0];

cout在
int*ptr=v;
的上下文中,数组
v
衰减为
&v[0]
。在
sizeof(v)
的上下文中,它仍然是一个数组。

\include
#include <stdio.h>
int main()
{
    printf("%zu\n", sizeof(int));
    // 4 (on a 64 bit arch)
    printf("%zu\n", sizeof(int*));
    // 8 (on a 64 bit arch)

    int v[5] = {0};

    /*Arrays are real and sizeof digs that.
      sizeof v == sizeof(int) * 5 
      */
    printf("%zu\n", sizeof v);
    // 20

    /*But,
      they convert to pointers to the first element 
      in pointer arithmetic expressions and in function calls
    */

    assert( v+0 == &v[0] );

    /*
       &v[0] != &v 

       While they're the same numerically:

    */
    assert ( (void*)&v == (void*)&v[0] );
#if 0
    /*They have distinct types and so this doesn't compile*/

    assert ( &v == &v[0] );
#endif

    /*The difference shows in pointer arithmetic*/

    /*adding 1 to v (== &v[0]) increments the pointer by sizeof(int) (== sizeof(v[0]) ) */
    assert ( (void*) (&v[0] + 1)  == (char*)&v[0] + sizeof(v[0]) );

    /*whereas adding 1 to &v increments the pointer by sizeof v (==5 * 4)*/

    assert ( (void*) (&v + 1)  == (char*)&v[0] + sizeof(v) );

    puts("success");
    return 0;
}
int main() { printf(“%zu\n”,sizeof(int)); //4(在64位arch上) printf(“%zu\n”,sizeof(int*); //8(在64位arch上) int v[5]={0}; /*数组是真实的,其大小是真实的。 sizeof v==sizeof(int)*5 */ printf(“%zu\n”,大小为v); // 20 /*但是,, 它们转换为指向第一个元素的指针 在指针算术表达式和函数调用中 */ 断言(v+0==&v[0]); /* &v[0]!=&v 虽然它们在数字上是相同的: */ 断言((void*)和v==(void*)和v[0]); #如果0 /*它们有不同的类型,因此无法编译*/ 断言(&v==&v[0]); #恩迪夫 /*不同之处体现在指针算法上*/ /*将1添加到v(=&v[0])会将指针增加sizeof(int)(==sizeof(v[0]))*/ 断言((void*)(&v[0]+1)==(char*)&v[0]+sizeof(v[0]); /*而将1添加到&v会使指针的大小增加v(=5*4)*/ 断言((void*)(&v+1)==(char*)&v[0]+sizeof(v)); 看跌期权(“成功”); 返回0; }
OHHH…我错过了它为什么用C来标记?它只是C++,正如它出现的一样。<代码> PTR>代码>和<代码> PTR2<代码>。在初始化它们之后,你再也不用它们了。所以,我不清楚你在问什么。好的,谢谢。还有一个问题。我如何用新的操作符来定位这样的东西:int?(*p)[5]=什么的新特性?