Cosizzle Posted November 6, 2009 Share Posted November 6, 2009 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 or length 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 Quote Link to comment https://forums.phpfreaks.com/topic/180611-getting-c-array-length/ Share on other sites More sharing options...
Daniel0 Posted November 7, 2009 Share Posted November 7, 2009 You can't. Arrays are just short hand notation for pointers. When you pass an array, you are passing the memory address of the first element. Say you have an array of 10 chars for instance. One char takes up exactly 1 byte, so an array of 10 chars take up 10 bytes. These are all stored sequentially in memory, so when you initialize a char array of size 10, you are allocating 10 byte somewhere in memory. This means the following things are equivalent for your char array: arr[0] arr* or: (arr + 0)* arr[1] (arr+1)* arr[2] (arr+2)* arr[n] (arr+n)* etc. So even though the highest index in this array is only 9, doing arr[1000] is valid because (arr+1000)* is valid even though it's out of bounds and you might really be reading anything. This is also why array indices start from 0 in case you ever wondered. The solution is to pass the size of your array as an extra argument to the function. So, something like char *argv[] and char **argv (which is often seen as argument in the main function) are entirely equivalent. This is also the reason why you have the argc argument: you don't know how long argv is otherwise! Quote Link to comment https://forums.phpfreaks.com/topic/180611-getting-c-array-length/#findComment-953149 Share on other sites More sharing options...
Andy-H Posted January 3, 2010 Share Posted January 3, 2010 If the array is null terminated you can use this: #include <iostream> using namespace std; int arrayLen(char *arr); int main() { char characterArray[6] = { 'a', 'b', 'c', 'd', 'e', '\0' }; cout << endl << "Array Length: " << arrayLen(&characterArray) << endl; return 0; } int arrayLen(char *arr) { register int total = 0; for (; *arr[total]; total++); return i; } Don't take my word on that tho, I have only really been learning C++ for about a week. Haven't even finished the book I'm using lol Quote Link to comment https://forums.phpfreaks.com/topic/180611-getting-c-array-length/#findComment-987745 Share on other sites More sharing options...
greatstar00 Posted February 13, 2010 Share Posted February 13, 2010 Andy-H, in his example, he is passing a int to the function so, your point definitely dont work Quote Link to comment https://forums.phpfreaks.com/topic/180611-getting-c-array-length/#findComment-1011692 Share on other sites More sharing options...
Andy-H Posted February 14, 2010 Share Posted February 14, 2010 You could use Vectors ? Quote Link to comment https://forums.phpfreaks.com/topic/180611-getting-c-array-length/#findComment-1012166 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.