没有 [ ] 的数组等于什么?C++
What does an array equal to without the [ ]? c++
由于某种原因,打印出的第一个数字不遵守主函数中的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
后没有放置行尾字符。您在输出中看到的"奇怪数字"实际上是重复两次的数组的初始元素。
它应该工作得很好,除了第一个输出与以下输出混合。为了更清楚地看到它,您应该在打印出第一个元素后放置一个换行符:
改变
cout << *augArray; // print the first element
自
cout << *augArray << endl; // print the first element and go to a new-line
现场观看:http://ideone.com/y16NeP。
旁注:您可以简单地将i++/j++
放在for
行上,例如for (int i = 0; i < 5; i++)
。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 什么时候在C++中返回常量引用是个好主意
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++避免重复声明的语法是什么
- c++库的公共头文件中应该包含什么
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- ifstream什么都没读
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 我应该使用什么来代替void作为变体中的替代类型之一
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用