简体   繁体   English

使用数组无法获得正确的值

[英]can't get right value with array

Q 1. 问1。

I want to print the value of even[0] to be 10 with this code below. 我想使用下面的代码将even [0]的值打印为10。

#include <iostream>

using namespace std;

int main()
{
int odd[10] = {1, 3, 5, 7, 9};
int even[1];
even[0]=(int)odd[0]+(int)odd[9];
cout<<even[0];
cin.get();

return 0;
}

but I get this output instead. 但我得到了这个输出。

1

What am I doing wrong? 我究竟做错了什么?

Q 2. 问2。

But when I change the code to 但是当我将代码更改为

#include <iostream>

using namespace std;

int main()
{
int odd[10] = {1, 3, 5, 7, 9};
int even[1];
even[0]=(int)odd[0]+(int)odd[9];
cout<<even;  // No index
cin.get();

return 0;
} 

I get this output. 我得到了这个输出。

001EFD94
int odd[10] = {1, 3, 5, 7, 9};

initialized odd to {1,3,5,7,9,0,0,0,0,0} , (int)odd[0]+(int)odd[9] will get 1 初始化odd{1,3,5,7,9,0,0,0,0,0}(int)odd[0]+(int)odd[9]将得到1

cout<<even;

will print address of even , you want 将打印的地址even ,你想

std::cout<<even[0] << std::endl;

§8.5.1.5 §8.5.1.5

To zero-initialize an object or reference of type T means: 零初始化T类型的对象或引用意味着:

— if T is a scalar type (3.9), the object is set to the value 0 (zero), taken as an integral constant expression, converted to T;103 - 如果T是标量类型(3.9),则将对象设置为值0(零),作为整数常量表达式,转换为T; 103

— if T is a (possibly cv-qualified) non-union class type, each non-static data member and each base-class subobject is zero-initialized and padding is initialized to zero bits; - 如果T是(可能是cv限定的)非联合类类型,则每个非静态数据成员和每个基类子对象都是零初始化的,并且填充初始化为零位;

— if T is a (possibly cv-qualified) union type, the object's first non-static named data member is zeroinitialized and padding is initialized to zero bits; - 如果T是(可能是cv限定的)联合类型,则对象的第一个非静态命名数据成员为零初始化,并且填充初始化为零位;

— if T is an array type, each element is zero-initialized; - 如果T是数组类型,则每个元素都是零初始化的;

— if T is a reference type, no initialization is performed. - 如果T是引用类型,则不执行初始化。

§8.5.1.7 §8.5.1.7

7 To value-initialize an object of type T means: 7要初始化T类型的对象,意味着:

— if T is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor); - 如果T是具有用户提供的构造函数(12.1)的(可能是cv限定的)类类型(第9节),则调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化是错误的);

— if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if T's implicitly-declared default constructor is non-trivial, that constructor is called. - 如果T是一个(可能是cv限定的)非联合类类型而没有用户提供的构造函数,那么该对象是零初始化的,如果T的隐式声明的默认构造函数是非平凡的,则调用该构造函数。

if T is an array type, then each element is value-initialized; - 如果T是数组类型,则每个元素都是值初始化的;

otherwise, the object is zero-initialized. - 否则,对象被零初始化。

odd[9]正在拉一个随机的内存块,因为你没有初始化那个远的数组。

The odd array has enough room for 10 values, but only the first 5 are initialized explicitly. odd阵列有足够的空间容纳10个值,但只有前5个明确初始化。 The others are implicitly initialized to 0. So 其他的被隐式初始化为0.所以

int odd[10] = {1, 3, 5, 7, 9};

has the same effect as 具有相同的效果

//     index:  0  1  2  3  4  5  6  7  8  9
int odd[10] = {1, 3, 5, 7, 9, 0, 0, 0, 0, 0};

So the line 所以行

even[0] = (int) odd[0] + (int) odd[9];

sets even[0] to 1 + 0. I think you want even[0]为1 + 0.我想你想要

even[0] = odd[0] + odd[4];

Note that, since odd and even are both declared to be of type int , you don't need to cast the values before you use them. 请注意,由于oddeven都声明为int类型,因此在使用它们之前不需要转换值。

When you print even without an index, you're printing the address of the array, rather than any of its contents. even没有索引也打印时,您打印的是数组的地址 ,而不是其中的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM