Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Sep 21st, 2006, 3:21 AM   #11
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
alright, so you've got the recursive solution...it seems like you're having a clerical problem rather than a real logic problem. try using the dereferencing operator (*) to ...

never mind, just looked at it again, think about sizeof.

sizeof(array[1024]) = sizeof(array[1]) or array[whatever], just the size of the data element in bytes. messing with that is not going to help you. also the "sizeof" the array itself is only going to be the # of bytes required to hold a pointer to the array. quit fucking with sizeof. it won't tell you how many elements are there.

this discrepancy has led me to believe that you may have even incorrectly used array bracket notation in your original post as you apparently are unclear on some stuff. (not trying to be mean, i'm unclear on a lot of stuff)

maybe restate the question in plain english rather than (pseudo)code?

good luck man!
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Sep 22nd, 2006, 8:09 PM   #12
RemoteC2
Programmer
 
RemoteC2's Avatar
 
Join Date: Jan 2006
Posts: 55
Rep Power: 3 RemoteC2 is on a distinguished road
okay. i will restate my question in plain english: how can i pass arrays of any size to a function?
RemoteC2 is offline   Reply With Quote
Old Sep 22nd, 2006, 8:25 PM   #13
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 880
Rep Power: 4 The Dark is on a distinguished road
You can just declare the parameter as
function (int array[])
however this does not tell the function anything about the size of the array.
Your function either needs to know the size of the array, or you could use some sentinel value in the array to tell it when to stop - this doesn't apply in your case, as you can't have a sentinel value as your array elements can contain any integer.
So you could pass the size in, e.g.
function (int array[], int size)
which is pretty staightforward. Then you just call the function like
function(myArray, sizeof myArray / sizeof myArray[0])

If you don't want to do that, then you could use the std::vector class instead of an array. The std::vector class has member functions that you can use to find the number of elements or to iterate through all the elements.
The Dark is offline   Reply With Quote
Old Sep 22nd, 2006, 11:11 PM   #14
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by The Dark View Post
So you could pass the size in, e.g.
function (int array[], int size)
which is pretty staightforward. Then you just call the function like
function(myArray, sizeof myArray / sizeof myArray[0])
Everything else in DArk's post is good, but I have a problem with this particular bit of advice as it doesn't always work.

For example;
#include <iostream>

int function(int array[], int size)
{
     // print out the size and then the elements
     std::cout << "Size = " << size << '\n';
     for (int i = 0; i < size; ++i)
         std::cout << i << ' ' << array[i] << '\n';
}

int intermediary(int array[])
{
     function(array, sizeof array/sizeof array[0]);
}

int main()
{
     int x[] = {11,12,13,14,15};   // an array with 5 elements
     
     std::cout << "First try\n";
     function(x, sizeof x/sizeof x[0]);
     std::cout << "Second try\n";
     intermediary(x);
}
A superficial look at this code would suggest the first and second tries printing the array will achieve the same output, because the call of function() in main() is of the same form as the call of function() within intermediary(). The problem is that the function intermediary() only receives a pointer to the first element of the array, and receives no information about the size of the array.

Within main(), the expression "sizeof x/sizeof x[0]" will give the value 5 (as the compiler knows x is an array of 5 ints). That code works as intended.

However, the expression "sizeof array/sizeof array[0]" in intermediary() will not yield a value of 5 because array is received as a pointer, but comes with no information about the number of elements it is associated with. "sizeof array" therefore computes the size of the pointer. Let's assume we're on a 32 bit OS. The size of a pointer is typically 4 bytes and the size of an integer is often (not always though) 4 bytes on a 32 bit system. So "sizeof array/sizeof array[0]", on a lot of 32 bit systems will yield a value of 1.
grumpy is offline   Reply With Quote
Old Sep 22nd, 2006, 11:51 PM   #15
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
An interesting behavior, grumpy. Perhaps a good canidate for use in the Underhanded C Contest.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Sep 23rd, 2006, 3:23 AM   #16
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
"What is NOT a pointer"
__________________
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 24th, 2006, 3:18 AM   #17
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
[,],{,},-,=,_,+,<,>,!,%,&,*,(,)

yeah, they'll act gay. so does the problem logic. for every minute you spend trying to figure out why something doesn't work, you'll save thousands of hours in the future.

keep plugging away man!
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja 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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:30 PM.

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