Thread: array length

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    44

    array length

    I realized that strlen() only works on arrays of characters. I need to find a way to get the length of an array such as:
    Code:
    int array[10];
    
    array[0] = 1;
    array[1] = 1;
    array[2] = 1;
    The answer would obviously be 3.
    I came up with this cheat that works in most cases:
    Code:
    int length = 0;
    
    for (int x = 0; array[x]; ++x)
    {
        ++length;
    }
    
    return length;
    it works for the above example, but for cases like array[1] = 0; the for loop would terminate on array[1], causing the length to return as 1.

    What's a good way to find the length of any type of array?
    Thanks in advance.

    edit: Changing array[x]; to array[x] || array[x] == 0; won't work as there are a few ways my cheat can be disrupted.
    Last edited by Wick; 08-30-2003 at 02:19 PM.
    Check out all my dimensions:
    Height, width, and for a limited time only... Depth!
    -sb

  2. #2
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    As far as I know, you canīt do this. But to declare an array you must use a fixed size, either with static or dynamic allocation. I think that you could use the vector class if you want this kind of thing anyway!
    Nothing more to tell about me...
    Happy day =)

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Code:
    int len = sizeof(myArray) / sizeof(myArray[0]);
    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    This fails, however, when you pass the array to a function, which is why I never rely on that method.

    Wick, the best way is to not use arrays at all. Use a std::vector. It's as easy to use as an array, and you have the .size() method to tell you its size.

    Example:

    Code:
    #include <vector>
    #include <iostream>
    
    typedef std::vector<int> intArray;
    
    void printArray(intArray ia){
      for (int i = 0; i < (int) ia.size(); i++){
        std::cout << "[" << ia[i] << "]";
      }
    }
    
    int main(){
      intArray myArray(20); //creates an array of 20 elements
      for (int i = 0; i < (int) myArray.size(); i++)
        myArray[i] = 2* i;
      printArray(myArray);
    }
    In 95% of cases, std::vector can be substituted freely for an array with minimal effort, and it's the better choice to use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  2. the length of my array is not what I expected
    By luca in forum C Programming
    Replies: 7
    Last Post: 12-05-2006, 03:14 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM