Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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_Pointer Arithmetic - Fatal编程技术网

C++ 空指针和指针算法

C++ 空指针和指针算法,c++,arrays,pointers,pointer-arithmetic,C++,Arrays,Pointers,Pointer Arithmetic,有没有办法阻止下面的while循环在超过40后迭代?我试图在未找到NULL指针时复制迭代的链表概念 int main() { int* arr = new int[4]{ 10,20,30,40 }; //for(int i=0; i<4; ++i) -- works fine while (arr) { cout << *(arr++) << endl; } delete[] arr; //

有没有办法阻止下面的while循环在超过40后迭代?我试图在未找到
NULL
指针时复制迭代的链表概念

int main() {
    int* arr = new int[4]{ 10,20,30,40 };
    //for(int i=0; i<4; ++i) -- works fine
    while (arr) {
        cout << *(arr++) << endl;
    }
        
    delete[] arr; // Free allocated memory
    return 0;
}
intmain(){
int*arr=新的int[4]{10,20,30,40};
//对于(int i=0;i
是否有必要在超过40后停止下面的while循环迭代

有两种方法可以停止循环:使条件变为false,或跳出循环(中断、转到、返回、抛出等)。您的循环也不会这样做

您的条件是
arr
,只有当指针指向null时,该条件才为false。您从未将null分配给
arr
,因此它从未为null


我试图复制链表的概念

链表概念通常不适用于非链表的事物。数组不是链表

是否有必要在超过40后停止下面的while循环迭代

有两种方法可以停止循环:使条件变为false,或跳出循环(中断、转到、返回、抛出等)。您的循环也不会这样做

您的条件是
arr
,只有当指针指向null时,该条件才为false。您从未将null分配给
arr
,因此它从未为null


我试图复制链表的概念


链表概念通常不适用于非链表的事物。数组不是链表。

因为
arr
被放置在一个连续的内存中,所以在
arr
之后,您永远不会得到一个空的内存地址值

您可以试用联机编译器

#include <iostream>

int main()
{
    int* arr = new int[4]{ 10,20,30,40 };
    for(int i=0; i<4; ++i){
        std::cout << *(arr++) << std::endl;
        std::cout << arr << std::endl;
    }
    std::cout << "NULL is " << (int*)NULL; // NULL mostly stands for 0.
    return 0;
}
为什么linkedlist有效?因为linkedlist将数据存储在非连续内存中,
next()
将为您提供
NULL
作为列表结束的标志

也可能需要一本C++基础书籍。


这是.

因为
arr
被放置在一个连续的内存中,所以在
arr
之后,您永远不会得到一个空的内存地址值

您可以试用联机编译器

#include <iostream>

int main()
{
    int* arr = new int[4]{ 10,20,30,40 };
    for(int i=0; i<4; ++i){
        std::cout << *(arr++) << std::endl;
        std::cout << arr << std::endl;
    }
    std::cout << "NULL is " << (int*)NULL; // NULL mostly stands for 0.
    return 0;
}
为什么linkedlist有效?因为linkedlist将数据存储在非连续内存中,
next()
将为您提供
NULL
作为列表结束的标志

也可能需要一本C++基础书籍。


下面是一个例子。

使用保留值(如零)并将其附加到数组的末尾,就像使用旧的C字符串一样。这称为sentinel元素

int*arr=newint[4]{10,20,30,40,0};
while(*arr){
...

使用保留值(如零)并将其附加到数组末尾,就像使用旧的C字符串一样。这称为sentinel元素

int*arr=newint[4]{10,20,30,40,0};
while(*arr){
...

>P>int(4)是C++数组,相反,使用C++ STD::数组及其迭代器:

#include <array>
#include <iostream>

int main() // Print all items
{
    std::array<int, 4> arr{ 10, 20, 30, 40 };

    for (auto i : arr)
        std::cout << i << std::endl;
}
#包括
#包括
int main()//打印所有项目
{
std::数组arr{10,20,30,40};
用于(自动i:arr)

STD::CUT

INT[4 ]是C数组,而使用C++ STD::数组及其迭代器:

#include <array>
#include <iostream>

int main() // Print all items
{
    std::array<int, 4> arr{ 10, 20, 30, 40 };

    for (auto i : arr)
        std::cout << i << std::endl;
}
#包括
#包括
int main()//打印所有项目
{
std::数组arr{10,20,30,40};
用于(自动i:arr)

STD::指针指针在数组的末尾不会变为空。C++的答案不是:No.(ARR+4)不是空的。你可以尝试<代码> STD::Couth-链表概念适用于链接列表。动态数组在现代C++中是一个难闻的东西。你想要一个<代码>:STD::矢量< /代码>。<代码> int计数=4;而(计数){…}指针/指针不在数组的末尾变成空。C++不这样工作。简短回答:(不),(ARR + 4)不是空。你可以尝试<代码> STD::链接列表概念适用于,链接列表。动态数组在现代C++中是一个难闻的东西。你想要一个<代码>:STD::矢量< /代码>。<代码> int计数=4;而(计数){…}