Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 2nd, 2007, 5:29 PM   #1
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 234
Rep Power: 3 Soulstorm is on a distinguished road
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)
  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?
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old Sep 2nd, 2007, 5:41 PM   #2
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3 kruptof is on a distinguished road
Quote:
Originally Posted by Soulstorm View Post
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)
  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
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Sep 2nd, 2007, 5:50 PM   #3
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 234
Rep Power: 3 Soulstorm is on a distinguished road
Quote:
Originally Posted by kruptof View Post
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.
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old Sep 2nd, 2007, 5:52 PM   #4
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 290
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
Once you reach your function, all you have is a pointer to the first element in the array.
__________________
http://www.kevinherron.com/
andro is offline   Reply With Quote
Old Sep 2nd, 2007, 6:06 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Sep 2nd, 2007, 9:36 PM   #6
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
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.
Eoin is offline   Reply With Quote
Old Sep 2nd, 2007, 9:42 PM   #7
cryptod
Newbie
 
cryptod's Avatar
 
Join Date: Sep 2007
Location: Algeria
Posts: 2
Rep Power: 0 cryptod is on a distinguished road
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
cryptod is offline   Reply With Quote
Old Sep 3rd, 2007, 2:40 AM   #8
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 234
Rep Power: 3 Soulstorm is on a distinguished road
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)
Soulstorm is offline   Reply With Quote
Old Sep 3rd, 2007, 3:04 AM   #9
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 290
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
I'm interested, what gaps are you trying to fill?
__________________
http://www.kevinherron.com/
andro is offline   Reply With Quote
Old Sep 3rd, 2007, 8:13 AM   #10
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 234
Rep Power: 3 Soulstorm is on a distinguished road
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)
Soulstorm is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:38 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC