![]() |
Passing int arrays as arguments...
I have a function in which i need to pass as argument an integer array. But how can I know the size of the integer array once this is inside the function?
For example, the following code: :
Will have as a result the number 1. If I try to use the sizeof operator in the main(), it will have the expected results, however, once the pointer of the array is passed as an argument, it seems I cannot use the sizeof operator. Any ideas? |
Quote:
Edit: A simple search of the forum turned up a similar thread authored by me |
Quote:
|
Once you reach your function, all you have is a pointer to the first element in the array.
|
Let me express what Andro is saying in antoher way. You don't know the size. Wanting it to be otherwise doesn't work.
|
Since the language here is C++ and not plain C I'll add that you could use something like Boost.Array here to sidestep your dilemma.
But otherwise the answer given can't be argued with. If all the function receives is a pointer then that's all it gets. The size would require at least one additional argument. |
Its impossible to do it without passing the length of thr array as a parameter.
here is a possible solution : :
void foo(unsigned *p, int length)Good luck ;) |
That's what I have done until now. The only reason I am asking this is because I am making an Objective C library, filling some gaps left by Foundation Framework, but I am trying to preserve Objective C's strengths, like not knowing the size of any data structure that is passed as an argument. Anyway, if this is the only way, then that's how it's going to be done.
Thanks a lot, all of you. |
I'm interested, what gaps are you trying to fill?
|
Minor gaps. Well, I am building a framework, and during development of other projects, I had encountered many problems with Cocoa. For example, NSIndexSet and its mutable counterpart could really use a hand when it comes to directly accessing unsigned integers without having to do an iteration.
Or, NSMatrix, is there any function that returns the cound of cells? Or any function that directly tells each cell to perform a selected function? Or any function that sorts cells into a Custom View using a selector and prepares them for proper displaying? (that FS2_Open Launcher sure gave me hell while writting the dynamic menus) Also, some more support for C++ for use with OBJC++ could prove useful. Making some classes in C++ that are actually NSMutableArrays with overloaded operators would also be useful for ObjC++ programmers. Same with NSStrings. Also including automation for saving-loading files, additional routines for Cocoa Bindings when working with NSTableViews... The project is pretty much in infant stages, since I have a lot of stuff to addn and I have some other projects to do. Most of the implemented stuff are workarounds that were implemented into my projects. In what way are you interested? |
| All times are GMT -5. The time now is 2:51 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC