Determine the size of the array of structures that it used as a parameter

I am currently working on a project where I want to convert CSV file entries to a vector of objects. Therefore, I wrote a function that converts an array of structures into a vector. The problem is that right now my function only works if the user enters the correct array size as an additional parameter, but if he enters a larger number, an exception is thrown because the function is trying to read from an array record that does not exist. Now I want to know if there is anyway that I can determine the size of the array of structures in my function. I already tried sizeof(array)/sizeof(array[0]) , but this does not work.

Here is the function I'm talking about:

 BANKMANAGEMENT_API int initAccounts(ACCOUNT accArray_[], const int numOfAcc_) { BankmanagementClass *myBankmanagement = BankmanagementClass::createBankmanagementClass(); for (int i = 0; i < numOfAcc_; i++) { ACCOUNT acc = accArray_[i]; Account* newaccount = Account::accountStruct2Obj(&acc); myBankmanagement->setNextAccountId(myBankmanagement->determineNextId(newaccount->getAccountId(), myBankmanagement->getNextAccountId())); myBankmanagement->addAccount(newaccount); } LogInfo("Account Vector was initialized with data from Csv File."); return 0; } 

I want to get rid of the numOfAcc_ parameter numOfAcc_ that the user cannot enter the wrong size.

This is for dll with C interface.

+5
source share
1 answer

Unable to determine the size of the array. If you control how accArray filled, you can use the end marker and check this condition in a for loop.

0
source

Source: https://habr.com/ru/post/1258468/


All Articles