Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.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_Pointers - Fatal编程技术网

C++ 为什么这种在数组中导航的方法是错误的?

C++ 为什么这种在数组中导航的方法是错误的?,c++,arrays,pointers,C++,Arrays,Pointers,为了理解C指针,我设置了以下示例: int main() { int temp[] = {45,67,99}; int* address; address = &temp[0]; //equivalent to address = temp; std::cout << "element 0: " << *address << std::endl; address = (address + sizeof(i

为了理解C指针,我设置了以下示例:

int main()
{
    int temp[] = {45,67,99};    
    int* address;
    address = &temp[0]; //equivalent to address = temp;
    std::cout << "element 0: " << *address << std::endl;
    address = (address + sizeof(int));
    std::cout << "element 1: " << *address << std::endl;

    std::getchar();
    return 0;
}
为了将指针移动到数组的第二个元素,但我不明白为什么第一个方法是错误的。

运算符以字节为单位返回对象的大小。因此(取决于编译器),您可能刚刚完成了

address + sizeof(int) == address + 4

当您解除对指针的引用时,这显然会超出范围。问题是您尝试使用
sizeof
的方式已经在中说明。当您将整数(例如
1
)添加到
int*
时,它已经知道将
1 int
移到下一个地址。

将指针增加N不会增加
N
字节,而是增加
N*sizeof(pointed\u type)
字节

所以当你这样做的时候:

address = (address + sizeof(int));
您将地址增加(可能)4倍于int的大小,而int的大小超过了原始数组

您打算做的是:

address = reinterpret_cast<int*>(reinterpret_cast<char*>(address) + sizeof(int));
address=reinterpret_cast(reinterpret_cast(address)+sizeof(int));

这太可怕了。

数组是一组相同类型的元素,在内存中一个接一个地连续映射

阵列的大小为:

sizeof(any element) * the number of elements
因此,要获得数组的元素数,可以使用以下方法:

Number of elements = sizeof(array) / sizeof(any element).
  • 这些元素是可寻址的,每个元素都使用第一个字节的地址寻址

    int a[] = {1, 10, 100, 1000, 10000};
    std::cout << sizeof(int) << std::endl; // 4
    std::cout << sizeof(a) << std::endl; // 20: 5 * 4
    
如您所见,地址正以sizeof(int)//4递增

  • 要在途中完成,请执行以下操作:

    for(int* tmp = array; tmp <= a + nElement; tmp++)
        cout << tmp << " : ";
    
    正如你所看到的,结果是一样的

    • 要从一个元素移动到另一个元素,只需像我上面所做的那样增加/减少地址:

      tmp++; // incrementing the address by 1 means moving to the next element, element means an integer which means 4 bytes. 1 here is not a single byte but a unit or element which is 4 byte here;
      
    • 要移动到n元素,只需执行以下操作:

      tmp += n;
      
    在您的示例中:

        address = (address + sizeof(int)); : 
        address = address + 4; // moving  to the 0 + 4 element which means fifth element (here outbound).
    

    如果你知道地址=(地址+1)是正确的,那么为什么您希望
    address=(address+sizeof(int))是否也是正确的?当然,当你添加不同的数字时,你得到的结果也不一样。以前C++上的6个问题已经被否决了。有人正在进行一场向下投票的狂欢。如果你真的想提升
    sizeof(int)
    字节,那么指针应该被转换到
    char*
    ,然后再提升
    sizeof(int)
    @BaummitAugen,因为我看不出两者之间的区别,仅仅打印
    sizeof(int)
    可能会有所帮助。顺便说一句,不需要批准你文章中提出的每一个编辑,最后一个编辑会让事情变得更糟。@Ron当你转换到
    char*
    时,指针一次移动1个字节。然后在移动了正确的字节数后,您返回到
    int*
    ,以便在解引用操作期间正确解释新地址。@Ron我将询问者的问题解释为“我想将指针增加若干字节,为什么我不能这样做?”我向他演示了如何实现他的意图。
        0018FF38 : 0018FF3C : 0018FF40 : 0018FF44
    
    tmp++; // incrementing the address by 1 means moving to the next element, element means an integer which means 4 bytes. 1 here is not a single byte but a unit or element which is 4 byte here;
    
    tmp += n;
    
        address = (address + sizeof(int)); : 
        address = address + 4; // moving  to the 0 + 4 element which means fifth element (here outbound).