Jump to content
  • Advertisement
Sign in to follow this  
MadScientist

Passing an array with a struct to a function array argument

This topic is 2643 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
if arr is an array of type T FindValue also needs to take an array of type T. FindValue(T arr[], int size);

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[ ] 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.

Share this post


Link to post
Share on other sites
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);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!