Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

asdasd12345

Putting array into functions

This topic is 5319 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

I have a function that takes an array of integers, what I want to know is why you have to define an another array of integers before making the function call. for example : int Function(int Num[16]) { } int main() { int arr[]={0,1,0,2,0,3,0,4}; Function(arr); return 0; } why cant I just write, Function({0,1,0,2,0,3,0,4}); I dont know much about this stuff but when I make the in arr[] array arent I using up extra memory than I would be if I just wrote Function({0,1,0,2,0,3,0,4}). It seems a bit inefficient. I have read something about operator overloading, but Im sketchy on it, so could you overload operators to make it so you can write Function({0,1,0,2,0,3,0,4}) and have it compile, because Ive tried that and it doesnt work.

Share this post


Link to post
Share on other sites
Advertisement
this is what I did with your code:

void Function(int* Num)
{
*Num = 99;
}

int main()
{
int arr[]={0,1,0,2,0,3,0,4};
Function(arr);
cout << *arr;
getchar();
return 0;
}


when ''arr'' is passed into the function, the address is passed for the first element. So when you work with it you''re not making a second array..you are referencing the original one through a pointer. In the example above I pass the array with a pointer and make a change to the first element then exit off the function. In main I output ''arr'' and as expected its 99, which is proof that only one array is used, so no excessive memory overhead.

Share this post


Link to post
Share on other sites
Thanks, i see what your saying, I guess what annoys me is that you cant just say Function({0,1,0,2,0,3,0,4}); - just seems a bit weird to me, maybe theres a good reason for it and i just dont know what it is.

Share this post


Link to post
Share on other sites
The problem with using an aggregate initializer list like that is that the aggregate is untyped. You need at least some sort of type specifier in order to disambiguate the expression. In C++ there''s no way to declare the type of the aggregate without binding it to a variable.
However, with the 1999 revision of the C standard you can do something like:

void Function(int * p) {}

int main() {
Function((int [8]){0, 1, 0, 2, 0, 3, 0, 4});
return 0;
}

and have things compile.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!