Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
if i have declared the following structure
C++
struct fresh
{
    int value;
};


and declared the following 2 arrays;
C++
int my_array[4] = {50, 70, 800, 30};
struct fresh foo[4];


i want the values in my_array[i] to be variable and always take the value of foo[i].value even if foo[i].value is change

i don't know how to do that, any help

Thanks in advance,
z3ngew
Posted

If my_array must be of type int then that is going to be difficult to achieve. If, however, you can let my_array be of type int* then it can be done like this;

C++
#include<iostream>

struct fresh
{
    int value;
};

int main()
{
    fresh foo[4];

    int* my_array[4] = {
        &foo[0].value,
        &foo[1].value,
        &foo[2].value,
        &foo[3].value,
    };

    foo[2].value = 32;
    std::cout << "foo[2].value==" << foo[2].value << "==" << *my_array[2] << std::endl;

    return 0;
}


Also, remember arrays are evil[^] so if you want this to work with variable number of elements you're better off using a std::vector:
C++
#include<iostream>
#include<vector>

using namespace std;

struct fresh
{
    int value;
    fresh(const int& value) {
        this->value;
    }
};

int main()
{
    vector<fresh> foo;
    vector<int*> my_array;

    // Add variable number of fresh's to foo
    foo.push_back(fresh(1));
    foo.push_back(fresh(2));
    foo.push_back(fresh(3));
    foo.push_back(fresh(4));
    foo.push_back(fresh(5));
    foo.push_back(fresh(6));

    // Make sure my_array elements is pointing to value of fresh in foo
    for(int i = 0; i < foo.size(); ++i) {
        my_array.push_back(&foo[i].value);
    }

    foo[2].value = 32;
    std::cout << "foo[2].value==" << foo[2].value << "==" << *my_array[2] << std::endl;

    return 0;
}



Hope this helps,
Fredrik
 
Share this answer
 
Comments
[no name] 12-Aug-13 8:10am    
Using pointers is by far the most efficient as other solutions will require a copy.
Philippe Mori 14-Aug-13 8:44am    
fresh constructor should be fresh(int value) : value(value) { } or fresh(int value) { this->value = value; }. Assignation is missing in your sample.
Philippe Mori 14-Aug-13 8:48am    
Be aware that your second sample is fragile. If foo vector is modified, then my_array might become invalid.
You should uses a single array if possible.

In some cases, an union or reinterpret_cast can be used but it should generally be avoided in your own code as it can be hard to maintain and cause some subtil bugs if you don't understand all the implications.

A typical example where it might make some sense if for coordinates that might be represented either as an array or by a member (X, Y, Z).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900