gpt4 book ai didi

c++ - 没有 [ ] 的数组等于什么? C++

转载 作者:行者123 更新时间:2023-11-28 00:31:15 25 4
gpt4 key购买 nike

出于某种原因,打印出的第一个数字不遵守 main 函数中的 for 循环,告诉它范围为 0-10;但是当

void printArray(int augArray[5])
{
cout << *augArray; //this line is taken out, the for loop proceeds with normal
for (int i = 0; i < 5;) //outputs, when the first line is kept also the for loop only
{ //produces 4 other numbers, just curious to why this is
cout << augArray[i] << endl; //happening, thanks.
i++;
}
}

int main()
{
srand(time(0));
int anArray[5];

for(int j = 0; j < 5;)
{
anArray[j] = rand() % 11;
j++;
}

printArray(anArray);
cout << anArray;
}

最佳答案

当在表达式中使用数组名称时后面没有方括号,它的值等于指向数组初始元素的指针,即表达式中的 augArray&augArray[0]。因此,*augArray*(&augArray[0]) 相同,只是 augArray[0](星号和&符号相互抵消)。

输出看起来奇怪的原因是您在打印 *augArray 后没有放置行尾字符。您在输出中看到的“奇怪的数字”实际上是数组的初始元素重复了两次。

关于c++ - 没有 [ ] 的数组等于什么? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22853496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com