Reputation: 11
I have a dynamic array which will be updated from a different test system. I am havign hard time with syntax. Can someone please help me here?
Here is where I declare my vector
void TestSystem::Drive(U32 RunMode, U32 Options)
{ //U32 is unsigned 32 bit
U32 condition;
U32 i;
U8 k;
vector<U32> badDriveList(1,0);//initial size of vector is 1 and value is 0
int *badDrivePointer[&badDriveList];
bool driveStatus = testSystems[k]->DriveCheck(badDrivePointer);//should this be &badDrivePointer?
// left side to "->" is correct. After that i am not sure
}
Here is where I update my vector.
int TestSystem::DriveCheck(int *badDriveList)
{
U32 condition;
U32 i= 10;
U32 j = 0;
for(j;j<i;j++)
{
badDriveList[j] = (*i)->shelf;//right hand side of this is working when tested with complete code base
//I am not sure how to write values in to badDriveList vector using pointers
}
}
here is part of my .h file
int DriveCheck(int *);
Upvotes: 1
Views: 232
Reputation: 17624
You can do this to get a pointer to the data in your vector:
U32 *badDrivePointer = &badDriveList[0];
However, your vector only has a single element in it. Therefore in TestSystem::DriveCheck(U32 *badDriveList)
you can only write a single element, eg:
badDriveList[0] = something;
If you need to be able to write 10 elements into the vector from within TestSystem::DriveCheck()
, then you need to allocate those 10 elements first. You need to do that before you use &badDriveList[0] to get a pointer to the data in the vector, because resizing a vector can reallocate the data, making a pointer retrieved earlier invalid.
If you don't know how many elements you need to put into the vector, then you would be better off passing a reference to the vector to TestSystem::DriveCheck()
, so that you can update the vector directly (instead of via a pointer). For example:
int TestSystem::DriveCheck(vector<U32>& badDriveList)
{
badDriveList.push_back(something);
}
Upvotes: 1
Reputation: 881705
You cannot properly use a pointer to update a STL container -- rather, you need to use an iterator. The insert_iterator adapter in particular seems to be what you're looking for.
Upvotes: 2