简体   繁体   中英

can't get right value with array

Q 1.

I want to print the value of even[0] to be 10 with this code below.

#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.

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

cout<<even;

will print address of even , you want

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

§8.5.1.5

To zero-initialize an object or reference of type T means:

— 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

— 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;

— 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;

— if T is an array type, each element is zero-initialized;

— if T is a reference type, no initialization is performed.

§8.5.1.7

7 To value-initialize an object of type T means:

— 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);

— 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.

if T is an array type, then each element is value-initialized;

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. The others are implicitly initialized to 0. So

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] = 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.

When you print even without an index, you're printing the address of the array, rather than any of its contents.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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