Reputation: 969
I am fairly new to using pointers in C++ but I will try and explain what I want to do.
I have a class object Rx (receiver), and in my program I will be using multiple receivers at the same time. Each receiver has a vector of data (observations), for simplicity I am just using a vector of doubles. I also have an array of bools that determine which observations to use and I would like each receiver (as a member variable) have a pointer to this array. For example the first element in the array of bools will say "true or false use the first observation you have receiver".
Also, further in my code I would also like to point to an array of objects, would I follow the same procedure?
int main()
{
// The elements in this array are set in the code before
bool use_observations[100];
// I have chosen 3 for an example but in my actual code I have a vector
// of receivers since the quantity varies
Rx receiver_1, receiver_2, receiver_3;
// I would like to set the pointer in each receiver to point
// to the array use_observations
receiver_1.SetPointer(use_observations);
receiver_2.SetPointer(use_observations);
receiver_3.SetPointer(use_observations);
} // end of main()
My receiver class declarations and definitions:
class Rx{
public:
Rx(); // Constructor
Rx(const Rx& in_Rx); // Copy constructor
~Rx(); // Destructor
void SetPointer(bool* in_Array); // Function to set pointer to use_observation
private:
std::vector<double> data;
bool* pointer_to_array[10];
}; // end of class Rx
void Rx::SetPointer(bool* in_Array)`{*pointer_to_array`= in_Array);
This is where I get the problems, either it doesnt assign correctly (get lots of nulls or not assigned) or I get an error on pointer_to_array saying expression must be a modifiable value
I haven't bothered showing the constructor, copy constructor and Destructor. I know normally in the destructor you should delete the pointer however Rx does not own the data in the array so I do not want to delete it. Thanks for your help
EDIT** I have shown some code that I am using and what I get for results and I have modified SetPointer() to display some results
int main
{
bool use_observations [6] = {true, true, true, true, true, true};
Rx receiver_1;
receiver_1.SetPointer(use_observations);
}
void Rx::SetPointer(bool* in_Array)
{
*pointer_to_array = in_Array;
for(int i = 0; i < 6; i++)
{
if(*pointer_to_array[i] == true)
std::cout << "Good" << std::endl;
} // end of for loop
} // end of SetPointer()
When I debug and step over (*pointer_to_array = in_Array) I get the result {true, and 0xCCCCCCCC for the rest of the elements} and then on the second iteration of the for loop it crashes saying "Access violation reading location 0xCCCCCCCC
SECOND EDIT ** Thank you everyone for your help. @PaulMcKenzie pointed out in his implementation (in the comments) in Rx that I should have bool* pointer_to_array not bool* pointer_to_array[6] and that solved the issue. As well I should be pointing to the start of the array buffer, not a pointer to the array.
Upvotes: 0
Views: 372
Reputation: 35440
The issue is that you want a pointer to the start of the array buffer, not a pointer to the array.
class Rx{
public:
void SetPointer(bool* in_Array);
bool* pointer_to_array;
};
void Rx::SetPointer(bool* in_Array) {pointer_to_array = in_Array);
Note the removal of the *
.
Upvotes: 1