how to find length of array in visual c++ ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sumitgoyal76
    New Member
    • Feb 2012
    • 6

    how to find length of array in visual c++ ?

    like if we have an array
    int [] a = new int [20]
    int []b;
    b= new int [a.length];
    FOR(INT I=0; I<a.length ; i++)
    {
    b[i]=a[i];
    }
    this program is not working in visual c++ it keep saying me that there is no such method as (a.length) in visual c++
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    There is no a.length because a is the address of an int. Addresses don't have methods.

    There is no way to find the length of an array in C or C++. The most you can do is to find the sizeof a variable on the stack. In your case you used the new operator which creates the array on the heap. A sizeof(a) will get you the size of the address of the array.

    Read this: http://bytes.com/topic/c/insights/77...rrays-revealed

    Comment

    • sumitgoyal76
      New Member
      • Feb 2012
      • 6

      #3
      thanks weaknessforcats :) i am new to c++ but a.length method works in java so i was trying the same with c ++
      so there is no way to find length of array in c++ ? because sizeof (a) gives the total size of array it dont give any information about length or we can do like if it is char array then (sizeof(a) /8 )is it good idea?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        sizeof returns the size of the local variable.

        If your array is a local variable (on the stack) sizeof(array) will return hw big the array is in bytes. But if you create your array on the heap using new or malloc then all that's local is the address of the array so sizeof(array) now returns the size of the address of the array and not the array itself. Further, if you pass your array to a function as an argument, then all that's passed is the address of the array. So inside the function sizeof(array) again returns the sizeof the address.

        You need to maintain a variable that contains the number of elements and pass it around with the array address. This is the only method that works in all cases.

        Further, arrays are consdered very low level. C++ users are supposed to use vectors. The vector container is an array but all of the code you need to write to manage your own personal array is duplicated by the vector code. You should just use a vector and save yurself a lot of time writing code that's alreayd been written and debugged.

        Comment

        • HugoPeters
          New Member
          • Aug 2014
          • 1

          #5
          I figured this worked pretty well:

          in the header define this: const int ARRAYEMPTY = -858993460;

          then

          int arrayLengt(a[])
          {
          bool end = false;
          int i = 0;
          while(!end)
          {
          if (a[i] == ARRAYEMPTY)
          {
          end = true;
          }
          else
          {
          i++;
          }
          }
          return i;
          }

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            You want to be careful of that solution. It prevents -858993460 from being in the array and this is a valid int value.

            What is passed to the function is the address of the array. As far as the function knows, this is an int*. That is, a pointer to a single int. Incrementing from that address is an assumption. There is no such thing as a pointer to an array. This is because the name of the array is defined as the address of element 0. There is no way to figure out the number of elements from this address.

            If you use sizeof on the array to determine the number of elements in the array, the array must be on the stack. The function's stack in this example does not contain the array so the function can't use sizeof to figure out the number of elements.

            The only foolproof method is to use two arguments for the function. One for the address of the array and one for the number of elements. Whoever created the array knew the number of elements so it's important to pass that along.

            Comment

            • gomathipriya
              New Member
              • May 2013
              • 16

              #7
              Finding the length of any non-dynamically allocated array is easy. You simply write:

              int Array[ ] = { ... };

              unsigned int Array_Length( sizeof( Array ) / sizeof( Array[ 0 ] ) );

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Just keep in mind that only works on the stack in the function where the array is defined.

                Everywhere else you will need the number of elements since all you will have is the address of the array. This is called decay of array.

                Comment

                Working...