| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
getting array size
Hey guys,
Sorry for the simplistic question, I come from a PHP/Java background. What im trying to figure out is how I can find out the array size of an array being passed into a class function. so: int cmds[] = {5,5,4,5,9,2} ... turtleOne.processTurtleMoves(cmds); =====[in the class now]===== void TurtleGraphics:: processTurtleMoves(const int commands[]) { // how would I get the size here (6) // my understanding is that I should be able to go: cout << sizeof commands / sizeof(int); // however I get 1 as a result. Is this because of the constant? } Thank you kindly for your help! -Cody |
|
#2
|
|||
|
|||
|
Re: getting array size
sorry, I should be more specific - I'm looking for the array length.
|
|
#3
|
|||
|
|||
|
Re: getting array size
The reason why that doesn't work is that you cannot pass arrays to functions. You're only passing a pointer to it's first element.
But then why do you want to calculate the size of an arry ?. You know the size whan you create it.. And yes you have to pass that size to any functions that uses the array as an additional parameter. Kurt |
|
#4
|
|||
|
|||
|
Re: getting array size
Thank you for your reply.
I don't know the size when I create it because I'm not specifying a size - it's dynamic. In my example it's 6.. in my application its about 50 give or take. Why I need to calculate the length of the array is because each number in that array needs to do something. Therefore I need to loop through it, and within the loop create a switch statement that checks the current number and performs actions based on the case. |
|
#5
|
|||
|
|||
|
Re: getting array size
Quote:
Kurt |
|
#6
|
|||
|
|||
|
Re: getting array size
Yes... but that number could change, so opposed to hard coding that number in is there any way to have it dynamic.
|
|
#7
|
|||
|
|||
|
Re: getting array size
Quote:
You could do something like this Code:
int cmds[] = {5,5,4,5,9,2};
int num_cmds = sizeof(cmds) / sizeof(cmds[0]) ;
Kurt |
|
#8
|
|||
|
|||
|
Re: getting array size
The bottom line is that you can always know the array size at the point of creation, but it's necessary to pass that information around along with the array when using it elsewhere. This can either be done via a separate parameter (which is the common C approach), or by putting both the array pointer and the size into a class object (the C++ approach).
Two such classes are already provided by the C++ standard library: std::tr1::array for fixed-size arrays, and std::vector for dynamic-size arrays. |
|
#9
|
||||
|
||||
|
Re: getting array size
I suggest you take a look at this article: http://www.codeguru.com/cpp/cpp/cpp_...le.php/c15257/.
__________________
Marius BancilaHome Page | Blog My CodeGuru articles My latest articles: A TR1 Tutorial: array, tuple, unordered containers, random number generators, regex, smart pointers Customizable alert window Try my VSBuildStatus add-in for Visual Studio 2005, 2008 & 2010 (v1.1). I do not offer technical support via PM or e-mail. Please use vbBulletin codes. |
|
#10
|
||||
|
||||
|
Re: getting array size
If it is possible, You can send the size of array to called function as a second parameter like:
Code:
int cmds[] = {5,5,4,5,9,2}
int size = sizeof(cmds) / sizeof(cmds[0]) ;
...
turtleOne.processTurtleMoves(cmds, size);
.
.
.
void TurtleGraphics::processTurtleMoves(const int commands[], const int size)
{
cout << size;
}
|
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|