Jump to content

getting c++ array length


Cosizzle

Recommended Posts

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

  • 1 month later...

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

Link to comment
Share on other sites

  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.