![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
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. |
|
|
|
|
|
#12 |
|
Programmer
Join Date: Jan 2006
Posts: 55
Rep Power: 3
![]() |
okay. i will restate my question in plain english: how can i pass arrays of any size to a function?
|
|
|
|
|
|
#13 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 880
Rep Power: 4
![]() |
You can just declare the parameter as
function (int 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) 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. |
|
|
|
|
|
#14 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5
![]() |
Quote:
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);
}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. |
|
|
|
|
|
|
#15 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
An interesting behavior, grumpy. Perhaps a good canidate for use in the Underhanded C Contest.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#16 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
"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 |
|
|
|
|
|
#17 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
[,],{,},-,=,_,+,<,>,!,%,&,*,(,)
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|