0

I am working with propriety code in my iOS project and all I have access to is the header files for my project. How do you declare an array in C++ so that it will return an array when I make a call?

Here's the header file method,

 short WI_GetIDsFromList(int32_t module, int32_t *idsArray, uint32_t *count);

How do you declare an array to received an array of in32_t? I keep getting a parameter error for returnedIdsArray when I make this call? It works perfectly fine for count? I tried making it into a pointer but it did not work?

    //Array of ID's
    int32_t returnedIdsArray[] = {};

    // Array of ID's count
    uint32_t count;

 rc += WI_GetIDsFromList(mod, returnedIdsArray, &count);

Another Example

short dpCount;

//Get number of data points from the device
WI_GetDatapointCount(modIDHDS, &dpCount);

//dpCount now has returned value of method WI_GetDatapointCount
NSLog@"%d", int(dpCount);
7
  • what error do you exactly have? Commented Dec 6, 2017 at 23:11
  • @clyky invalid parameters. I don't think I am declaring these arrays right. Is a pointer needed for an array? Commented Dec 6, 2017 at 23:13
  • There is no memory there behind your pointers. Try to use int32_t **idsArray, if the function allocates memory to be returned, or provide an non zero length array as argument and indicate maximum length available in the count parameter. Commented Dec 6, 2017 at 23:17
  • do you mean "how do you declare a function to receive an array" ? Commented Dec 6, 2017 at 23:20
  • what is the meaning of the return value (being added to rc) ? Commented Dec 6, 2017 at 23:21

3 Answers 3

1

I think Mochi's question is how to declare the array that it suits the need of the function given in the header. If I understand him right, he has no influence to the function taking the array as parameter.

Did you try:

int32_t returnedIdsArray[MaximumExpectedIds];

Maybe there is also a function in the API giving you the number of Ids that you could use to determine the array size.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes. Let me try this, but you are correct. Like I said these are header files that are propriety code, it helps me access a hardware device. I can only use these methods declared in a header file based on their parameters. Thank you for understanding.
In case it does not work, please share the error message.
It worked! Thank you! The maximumExpectedIds are actually returned in a different method. I didn't know I just had to change the declaration.
1

You cannot pass an array in C or C++, because they will always decay to a pointer to the first element.

You can, however, pass a reference to an array. It retains its array type rather than decay to a pointer, so sizeof() will return the actual size of the array rather than the sizeof pointer, and so on.

void f(char(&charArray)[30])
{
}

Syntax is pretty ugly though. A type alias can help:

using CharArray30 = char(&)[30];
void f(CharArray30 charArray)
{
}

etc. It has restrictions, though. For example, you cannot pass arrays of a different size.

If you need your function to work with various sizes, you can use a function template with a non-type parameter for the size:

template <size_t SIZE>
void f(int32_t module, int32_t(&idArray)[SIZE])
{
  // ...
}

Comments

1

I guess that what you are trying to do is to have the function output a set of int values where the length is not known at compile-time.

In C++ an array has a fixed size that must be known at compile-time. The concept of "runtime-sized array" is called vector in C++.

Also, it is more natural to use the return value for values being returned. Your code could look like:

std::vector<int> WI_GetIDsFromList(int32_t mod);

and the calling code could be:

auto values = WI_GetIDsFromList(mod);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.