Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Passing int arrays as arguments... (http://www.programmingforums.org/showthread.php?t=13899)

Soulstorm Sep 2nd, 2007 6:29 PM

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:

:

  1. void foo(unsigned *p)
  2. {
  3.         cout << sizeof(p)/4 << '\n';
  4. }
  5.  
  6. int main (int argc, const char * argv[]) {
  7.         unsigned p[] = {1,2,3,4,5,6,7,8,9,0};
  8.         foo(p);
  9.     return 0;


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?

kruptof Sep 2nd, 2007 6:41 PM

Quote:

Originally Posted by Soulstorm (Post 133188)
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:

:

  1. void foo(unsigned *p)
  2. {
  3.         cout << sizeof(p)/4 << '\n';
  4. }
  5.  
  6. int main (int argc, const char * argv[]) {
  7.         unsigned p[] = {1,2,3,4,5,6,7,8,9,0};
  8.         foo(p);
  9.     return 0;


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?

Have the callee pass the size to you or loop through all the elements incrementing a variable as you go, until you reach a null (\0)

Edit: A simple search of the forum turned up a similar thread authored by me

Soulstorm Sep 2nd, 2007 6:50 PM

Quote:

Originally Posted by kruptof (Post 133192)
Have the callee pass the size to you or loop through all the elements incrementing a variable as you go, until you reach a null (\0)

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.

andro Sep 2nd, 2007 6:52 PM

Once you reach your function, all you have is a pointer to the first element in the array.

DaWei Sep 2nd, 2007 7:06 PM

Let me express what Andro is saying in antoher way. You don't know the size. Wanting it to be otherwise doesn't work.

Eoin Sep 2nd, 2007 10:36 PM

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.

cryptod Sep 2nd, 2007 10:42 PM

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 ;)

Soulstorm Sep 3rd, 2007 3:40 AM

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.

andro Sep 3rd, 2007 4:04 AM

I'm interested, what gaps are you trying to fill?

Soulstorm Sep 3rd, 2007 9:13 AM

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