![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3
![]() |
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: cpp Syntax (Toggle Plain Text)
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?
__________________
Project::Soulstorm (personal homepage) |
|
|
|
|
|
#2 | ||
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3
![]() |
Quote:
Edit: A simple search of the forum turned up a similar thread authored by me
__________________
Quote:
|
||
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3
![]() |
That won't work in my occasion, since the \0 character only goes at the end of a string. And I also think that wether an out-of-bounds bound location is null or not depends on the implementation. Anyway, seems from your thread that there isn't a way to do that.
__________________
Project::Soulstorm (personal homepage) |
|
|
|
|
|
#4 |
|
Professional Programmer
|
Once you reach your function, all you have is a pointer to the first element in the array.
__________________
http://www.kevinherron.com/ |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Let me express what Andro is saying in antoher way. You don't know the size. Wanting it to be otherwise doesn't work.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3
![]() |
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.
__________________
Visit my website BinaryNotions. |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Sep 2007
Location: Algeria
Posts: 2
Rep Power: 0
![]() |
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)
{
cout << length << '\n';
}
int main (int argc, const char * argv[]) {
unsigned p[] = {1,2,3,4,5,6,7,8,9,0};
int length=sizeof(p)/sizeof(p[0]) ;
foo(p,length);
return 0;
}Good luck ![]() |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3
![]() |
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.
__________________
Project::Soulstorm (personal homepage) |
|
|
|
|
|
#9 |
|
Professional Programmer
|
I'm interested, what gaps are you trying to fill?
__________________
http://www.kevinherron.com/ |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3
![]() |
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?
__________________
Project::Soulstorm (personal homepage) |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Combining languages | titaniumdecoy | Other Programming Languages | 12 | Jul 13th, 2006 2:03 PM |
| Arrays or Vectors? | can342man | C++ | 2 | Apr 20th, 2006 3:57 PM |
| Passing vectors as arguments | Soulstorm | C++ | 6 | Mar 18th, 2006 4:49 PM |
| Arrays in PHP | MrMan9879 | PHP | 6 | Jan 12th, 2006 9:18 PM |
| Passing pointers as function arguments? (C) | Duo | C++ | 6 | Mar 15th, 2005 3:20 PM |