Thread: Getting the length of an array

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dwks View Post
    I guess I'll just finish off by mentioning this . . . if you know that the argument you're passing is in fact an array (and not a pointer), you can use something like this.
    Finding the size wasn't the issue
    Passing it as an extra argument was!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You can, but the only issue I have against using code written like that is that invariably someone else will use my code and do something wrong with macro and pass in a pointer or something.

    Example:


    Code:
    void MyFunction(float vec[36])
    {
       // now what happens if you use vectorsum_array() here?
    }
    Some things don't always work as you would like...

  3. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I think one thing that makes the whole argument entirely invalid is the fact that most API's are what they are. And there is no way around passing in array sizes. So welcome to the way of the programming world, Pharao.

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by Elysia View Post
    Finding the size wasn't the issue
    Passing it as an extra argument was!
    Yeah, you're right. The issue was having to pass the number of elements in the array to the function. So why are we suggesting vectors? [edit] Or comtemplating suggesting them or whatever. [/edit] I think my suggestion's just as valid.

    But anyway, I don't think I'd ever use it myself, for the reason master5001 outlined.

    So welcome to the way of the programming world, Pharao.
    Or the C programming world, as Elysia would say.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #20
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You are right, C++ arrays are the same business, but at least you have vectors to fall back on. That and other standard template library features that address issues with other complicated areas of C programming. Even programs I write in Delphi use the same APIs as the ones I write in C/C++. My assembler programs that use dynamic memory have to deal with passing size as a parameter.. Its just a programming issue in general.

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are two distinct ways of knowing the length of an array [or string, list, or whatever]: Either you keep a count the number of items, or you mark the end. Both are equally valid, and each have their own good and bad points.

    And of course, C is also able to encapsulate data - you just can't as easily access the encapsulated data using member functions.

    For example, we can easily make a variable length float array:
    Code:
    struct floatarray
    {
        size_t len;
        float *data;
    };
    
    vectorsum(floatarray* array)
    {
        int i=0;
        float    sum=0;
        for(i=0;i<array->len;i++)
            sum+=array->data[i];
        return sum;
    }
    
    int main()
    {
        floatarray array;
        const int n = 100;
        int i;
        array.data = malloc(sizeof(float) * n);
        array.len = n;
    
        for(i = 0; i < n; i++)
           array.data[i] = 1.0 / (i+1);
        
        printf("sum = %5.2f\n", vectorsum(&array));
    
        return 0;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    After what mats just posted, I am not sure what else to add to this thread!

  8. #23
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Or the even more fun way to program these sorts of things:

    Example:
    Code:
    /* The type is more for logical understanding of what is going on that anything. */
    struct array_t
    {
        size_t length;
        char data[];
    };
    
    void *array_alloc(size_t size)
    {
        struct array_t *obj;
    
        if(!size)
            return 0;
    
        size += sizeof(struct array_t);
        
        obj = malloc(size);
    
        return (obj)?obj + 1:0;
    }
    
    void array_free(void *ptr)
    {
        struct array_t *obj = ptr;
    
        if(obj)
            free(obj - 1);
    }
    
    size_t array_size(void *ptr)
    {
        struct array_t *obj = ptr;
    
        if(obj)
            return (obj-1)->length;
        return 0;    
    }
    But that can get kind of annoying to have to incorporate. Its mostly similar to the initial thing I suggested.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dwks View Post
    Yeah, you're right. The issue was having to pass the number of elements in the array to the function. So why are we suggesting vectors? [edit] Or comtemplating suggesting them or whatever. [/edit] I think my suggestion's just as valid.
    Because regardless of how you turn it, you still have to pass the size in either hard-coded form or some sort of compile time expression or macro. With vectors, you don't

    Or the C programming world, as Elysia would say.
    ^_^
    At least until C dies (that's a big if, though) or APIs stop being C compatible...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    A lot of functional languages use container objects (like vectors or something like the examples posted by matsp and myself) to handle their own arrays. Somewhere the data must be stored. The C way of thinking about it, however, is that its the programmers problem to maintain that information. Not the environments. PASCAL/Delphi seem to handle arrays so much better than C. At least bounds checks are thoroughly respected. But I hate that language family so I won't even attempt to sound like I am backing it up.

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

Tags for this Thread