Results 1 to 6 of 6

Thread: Length of array

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    I was using it in a function which gave me 1, and the way I posted gave me 1, but if I do this:
    Code:
    cout << sizeof(arr) << "/" << sizeof(int) << "=" << sizeof(arr) / sizeof(int) << endl;
    Is there any way to use it in a function?
    Last edited by Wynd; Feb 14th, 2002 at 07:05 PM.
    Alcohol & calculus don't mix.
    Never drink & derive.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    unfortunately it seems to be the language, arrays are not types, sizeof() works on both types and arrays. The best you can do is make a macro.
    Code:
    //template <class T> 
    //int length (T t){return  sizeof(t) / sizeof(T);}; //returns 1
    
    #define length(x) (sizeof(x)/sizeof(x[0])) //returns 5
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Well, that's better than nothing, but it'll have to do. Thanks
    Alcohol & calculus don't mix.
    Never drink & derive.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    but don't forget that you can use this ONLY in the function where you declared the array, therefore rendering it useless, because there the size is already known to you, if not the program. To let the program re-determine the size is bad habit.

    PHP Code:
    #define length(x) (sizeof(x)/sizeof(x[0])) //returns 5

    void func(intarr);

    void main()
    {
      
    int ar[30];

      for(
    int i 0length(ar); i++)  // useless, because you know that length is 30
        
    ar[i] = 2;

      
    func(ar);
    }

    void func(intarr)
    {
      for(
    int i 0length(arr); i++)  // length doesn't work here
      
    {
        
    arr[i] *= 2;
      }

    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    i know.. it's the language, unlike C++ BORK indentifiers will contain all type information, so for instance
    ar[array(30)]
    func(ar)

    func#[@arr[31] ]

    will not compile, because container is of type mutable*randomaccess*(static size)*container(30)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Won't that add a memory requirement overhead?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width