Results 1 to 3 of 3

Thread: Length of Array in C

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026

    Length of Array in C

    Hey guys, I really need ur help on this one!

    How do I find out the length of an array using C?

    for instance...

    I have an array that has 10 values in it, how do I get this number programatically...

    thanx in advance,
    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can't, really.

    Unless you specifically have a 10 item array:
    Code:
    void somecode() {
        int blah[10];
    }
    ...in which case you can use (sizeof(blah) / sizeof(int)) to get the number of items. If you've used malloc to obtain a pointer to a memory location, then there's no way, and you need to keep track of the size yourself.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    And if you have int blah[10] then there's no reason to compute the size. Note also that this only works in the function you declared the array.
    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