So I have an array with a struct so for example arr[x].num where num is an integer and i have a function void FindValue(int arr[],size).
I tried to pass the array to the function like so FindValue(arr[x].num,x) it says that there's a syntax error ']'. How do I pass the array correctly?
Using c++ and visual studio.
Passing an array with a struct to a function array argument
if arr is an array of type T FindValue also needs to take an array of type T. FindValue(T arr[], int size);
if arr is an array of type T FindValue also needs to take an array of type T. FindValue(T arr[], int size);
So this is true even though the array being passed contains integers within the struct?
Ok another question, could I just make a local array, temparr[x] and assign it the value of arr[x].num like so: temparr[x]=arr[x].num and then just pass temparr[x] to the function?
[ ] doesn't mean anything when you use it like that. It's a special notation that is only valid when specifying a function parameter that means you don't know the size of the array. You cannot use [ ] notation anywhere but in the definition/declaration of a function.
In any case, you really ought to be using std::vector, not raw arrays.
In any case, you really ought to be using std::vector, not raw arrays.
You need to make the conversion for each element manually
#include <iostream>
struct T
{
int id;
};
const int SIZE = 3;
void FindValue(int arr[], int size)
{
}
int main()
{
T physobj[3];
int temparr[SIZE];
for (int i = 0; i < SIZE; i++)
{
temparr = physobj.id;
}
FindValue(temparr, SIZE);
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement