Aesis
Aesis

Reputation: 31

How do I stop my array from printing address?

I have a header, cpp and main class.

//Arr.h
class Arr
{
public:
    void setArr();
    void printArr();
private:
    int x[5];
};

//Arr.cpp    
#include "Arr.h"
#include <iostream>

using namespace std;

void Arr::setArr()
{   
    int x[5] = { 2, 3, 5, 7, 11 };
}

void Arr::printArr()
{
    for (int i = 0; i < 5; i++)
    {
        cout << x[i] << "\n";
    }
}

//main.cpp
int main()
{
Arr a;
a.setArr();
a.printArr();
}

However, when I run the code, a.printArr() prints out array address and not the values contained in the array. Is there a way to fix this?

Upvotes: 0

Views: 586

Answers (4)

Viliami Mahe
Viliami Mahe

Reputation: 23

As WhiZTiM has already pointed out, your problem is at the setArr function in your class. The reason for this is because in C++ you cannot assign values to the array in the "normal fashion" i.e. using x = {blah, blah, blah}; (unless you use std::array<int, ARRAYSIZE>). In setArr you are creating another array named x and then this array is deleted once it is out of scope, and in printArr you are printing an array with no values in it yet.

Each value in the array must be set individually in C styled arrays (as shown in MikeCAT's answer).

One solution to this is to create a temporary array with the values you want and assigning the values to your class array through a for loop, i.e.:

void Arr::setArr() {
    const int ARRAYSIZE = 5;
    int tmp[ARRAYSIZE] = {2, 3, 5, 7, 11};
    for (int i = 0; i < ARRAYSIZE; ++i) {
        x[i] = tmp[i];
    }
}

As M.M also pointed out, can simply change int x[5] in your private variables in Arr to std::array<int, 5> x and then in setArr simply have the statement: x = {2, 3, 5, 7, 11}; which is the better option in my opinion and easier to read, but it's also good to know how to use C arrays 😊

Upvotes: 0

M.M
M.M

Reputation: 141618

Your code is not printing array address. Maybe you saw some unexpected numbers and assumed it was an address, but in fact it is undefined behaviour caused by outputting uninitialized variables.

Your code int x[5] = .... failed because this declares a new local variable x, it doesn't modify the class member x.

Perhaps you tried:

x = { 2, 3, 5, 7, 11 };

and got compilation errors. This is because C-style arrays may not be assigned. This annoying rule is a hangover from C++'s past. To avoid this problem and many others, you can try to avoid using C-style arrays. In this case use a C++ array:

private:
    std::array<int, 5> x;

Then the suggested assignment will work.

Upvotes: 0

WhiZTiM
WhiZTiM

Reputation: 21576

Your problem is here:

void Arr::setArr()
{   
    int x[5] = { 2, 3, 5, 7, 11 };
}

the declaration int x[5] defines a new array of 5 elements which will be destroyed on exiting that function; and the name x hides the data member Arr::x.

One way you can do it is this:

void Arr::setArr()
{   
    /*static*/ auto arr = { 2, 3, 5, 7, 11 };
    std::copy(arr.begin(), arr.end(), x);
}

Full code:

#include <iostream>
#include <algorithm>

class Arr
{
public:
    void setArr();
    void printArr();
private:
    int x[5];
};

using namespace std;

void Arr::setArr()
{   
    /*static*/ auto arr = { 2, 3, 5, 7, 11 };
    std::copy(arr.begin(), arr.end(), x);
}

void Arr::printArr()
{
    for (int i = 0; i < 5; i++)
    {
        cout << x[i] << "\n";
    }
}

//main.cpp
int main()
{
Arr a;
a.setArr();
a.printArr();
}

Upvotes: 0

MikeCAT
MikeCAT

Reputation: 75062

Your code will print not address but some indeterminate value generated via default-initializing. Initialize the member array instead of the local array to throw away.

void Arr::setArr()
{
    x[0] = 2;
    x[1] = 3;
    x[2] = 5;
    x[3] = 7;
    x[4] = 11;
}

Upvotes: 1

Related Questions