How do I get a count of the values in a array?

Discussion in 'C Programming' started by gk245, Mar 16, 2006.

  1. gk245

    gk245 Guest

    For instance, if i had a array like this:

    array[] = {1, 2, 3, 5, 6, ......};

    How do i tell C go get me the number of values in the array? Not the
    value itself, but how MANY of them there are.

    Thanks, please don't give me anything fancy. Since i can't use other
    functions to give that answer, just for loops and such.
    gk245, Mar 16, 2006
    #1
    1. Advertising

  2. gk245

    Ben Pfaff Guest

    gk245 <> writes:

    > For instance, if i had a array like this:
    >
    > array[] = {1, 2, 3, 5, 6, ......};
    >
    > How do i tell C go get me the number of values in the array? Not the
    > value itself, but how MANY of them there are.


    sizeof array / sizeof *array

    (This will work only for an actual array, not for a pointer into
    an array.)
    --
    "...deficient support can be a virtue.
    It keeps the amateurs off."
    --Bjarne Stroustrup
    Ben Pfaff, Mar 16, 2006
    #2
    1. Advertising

  3. gk245

    gk245 Guest

    Ben Pfaff wrote on 3/16/2006 :
    > gk245 <> writes:
    >
    >> For instance, if i had a array like this:
    >>
    >> array[] = {1, 2, 3, 5, 6, ......};
    >>
    >> How do i tell C go get me the number of values in the array? Not the
    >> value itself, but how MANY of them there are.

    >
    > sizeof array / sizeof *array
    >
    > (This will work only for an actual array, not for a pointer into
    > an array.)


    Thx, but like i said, i can't use a function to do this. sizeof is a
    function, right?
    gk245, Mar 16, 2006
    #3
  4. gk245

    Ben Pfaff Guest

    gk245 <> writes:

    > sizeof is a function, right?


    No. sizeof is an operator.
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Mar 16, 2006
    #4
  5. gk245

    gk245 Guest

    Ben Pfaff wrote on 3/16/2006 :
    > gk245 <> writes:
    >
    >> sizeof is a function, right?

    >
    > No. sizeof is an operator.


    lol, sorry. Yeah, unfortunately can't use that either. Haven't
    covered it yet. :(
    gk245, Mar 16, 2006
    #5
  6. gk245 wrote:
    > Ben Pfaff wrote on 3/16/2006 :
    > > gk245 <> writes:
    > >
    > >> sizeof is a function, right?

    > >
    > > No. sizeof is an operator.

    >
    > lol, sorry. Yeah, unfortunately can't use that either. Haven't
    > covered it yet. :(


    Put a sentinel value at the end that isn't a legal value. For example,
    if your
    array can only contain non-negative integers, put -1 at the end. If it
    can
    only contain non-null pointers, put NULL at the end. etc.

    -David
    David Resnick, Mar 16, 2006
    #6
  7. gk245 wrote:
    >
    > Ben Pfaff wrote on 3/16/2006 :
    > > gk245 <> writes:
    > >
    > >> sizeof is a function, right?

    > >
    > > No. sizeof is an operator.

    >
    > lol, sorry. Yeah, unfortunately can't use that either. Haven't
    > covered it yet. :(


    Translation:

    This is a homework assignment, and we aren't allowed to use anything
    we haven't covered yet in class.

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Mar 16, 2006
    #7
  8. gk245

    Ben Pfaff Guest

    gk245 <> writes:

    > Ben Pfaff wrote on 3/16/2006 :
    >> gk245 <> writes:
    >>
    >>> sizeof is a function, right?

    >>
    >> No. sizeof is an operator.

    >
    > lol, sorry. Yeah, unfortunately can't use that either. Haven't
    > covered it yet. :(


    Then use a macro:
    #define ELEM_CNT 5
    int array[ELEM_CNT] = {1, 2, 3, 4, 5};
    Thereafter, just use ELEM_CNT as the number of elements in the
    array.

    --
    "Your correction is 100% correct and 0% helpful. Well done!"
    --Richard Heathfield
    Ben Pfaff, Mar 16, 2006
    #8
  9. gk245 wrote:
    > For instance, if i had a array like this:
    >
    > array[] = {1, 2, 3, 5, 6, ......};
    >
    > How do i tell C go get me the number of values in the array? Not the
    > value itself, but how MANY of them there are.


    int main(void)
    {
    int array[] = {1, 2, 3, 5, 6};
    size_t nelements = sizeof array / sizeof *array;
    return 0;
    }
    Martin Ambuhl, Mar 16, 2006
    #9
  10. gk245 wrote:

    > Thx, but like i said, i can't use a function to do this. sizeof is a
    > function, right?


    NO. Please open your damn C text. Start at page one.
    Martin Ambuhl, Mar 16, 2006
    #10
  11. gk245

    gk245 Guest

    Martin Ambuhl wrote :
    > gk245 wrote:
    >
    >> Thx, but like i said, i can't use a function to do this. sizeof is a
    >> function, right?

    >
    > NO. Please open your damn C text. Start at page one.


    take it easy man. I had forgotten since there is quite a lot to learn
    in the beginning.
    gk245, Mar 16, 2006
    #11
  12. gk245

    CBFalconer Guest

    gk245 wrote:
    > Ben Pfaff wrote on 3/16/2006 :
    >> gk245 <> writes:
    >>
    >>> sizeof is a function, right?

    >>
    >> No. sizeof is an operator.

    >
    > lol, sorry. Yeah, unfortunately can't use that either. Haven't
    > covered it yet. :(


    Then you have no way. So go ahead and use it. That's why it is in
    the language.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
    CBFalconer, Mar 16, 2006
    #12
  13. the numbers in an array is equal to:

    sizeof(array)/sizeof(array[0])

    for example:

    main()
    {
    float array[]={3.14,-9.21,1.7e2,-99,0.12};

    printf("%d\n",sizeof(array)/sizeof(array[0]));
    }
    =?utf-8?B?6Zuo6Iqx55+zYw==?=, Mar 17, 2006
    #13
  14. gk245

    Richard Bos Guest

    CBFalconer <> wrote:

    > gk245 wrote:
    > > Ben Pfaff wrote on 3/16/2006 :
    > >> gk245 <> writes:
    > >>
    > >>> sizeof is a function, right?
    > >>
    > >> No. sizeof is an operator.

    > >
    > > lol, sorry. Yeah, unfortunately can't use that either. Haven't
    > > covered it yet. :(

    >
    > Then you have no way.


    Actually, I believe that since we already have a well-defined array to
    apply this to, as well as well-defined members of this array, there is a
    way to do this. It is, however, not something a beginning student should
    even attempt, since it's likely to teach bad habits.

    Richard
    Richard Bos, Mar 17, 2006
    #14
  15. gk245

    Robben Guest

    I guess this could be an interview question to test your knowledge of
    C. For Arrays in C there is no way to find the count of the number of
    elements of an array without using sizeof operator.

    If you use a for loop you need to know the size earlier as C allows to
    access the elements beyond the maximum array length; and it can fall
    into an infinite loop.

    gk245 wrote:
    > For instance, if i had a array like this:
    >
    > array[] = {1, 2, 3, 5, 6, ......};
    >
    > How do i tell C go get me the number of values in the array? Not the
    > value itself, but how MANY of them there are.
    >
    > Thanks, please don't give me anything fancy. Since i can't use other
    > functions to give that answer, just for loops and such.
    Robben, Mar 17, 2006
    #15
  16. "Robben" <> writes:
    > gk245 wrote:
    >> For instance, if i had a array like this:
    >>
    >> array[] = {1, 2, 3, 5, 6, ......};
    >>
    >> How do i tell C go get me the number of values in the array? Not the
    >> value itself, but how MANY of them there are.
    >>
    >> Thanks, please don't give me anything fancy. Since i can't use other
    >> functions to give that answer, just for loops and such.

    >
    > I guess this could be an interview question to test your knowledge of
    > C. For Arrays in C there is no way to find the count of the number of
    > elements of an array without using sizeof operator.
    >
    > If you use a for loop you need to know the size earlier as C allows to
    > access the elements beyond the maximum array length; and it can fall
    > into an infinite loop.


    Robben, please don't top-post. I've corrected it here.

    Actually, there are ways to compute the size of an array without using
    sizeof. They involve some ugly pointer arithmetic and casts to char*.
    I'm not going to go into details because (a) the OP didn't want
    anything fancy, and (b) there's no good reason not to use sizeof.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Mar 17, 2006
    #16
  17. On 17 Mar 2006 03:07:48 -0800, "Robben" <>
    wrote:

    >I guess this could be an interview question to test your knowledge of
    >C. For Arrays in C there is no way to find the count of the number of
    >elements of an array without using sizeof operator.
    >
    >If you use a for loop you need to know the size earlier as C allows to
    >access the elements beyond the maximum array length; and it can fall
    >into an infinite loop.


    While accessing an element beyond the maximum array length need not be
    a syntax error, C does NOT allow it. Any attempt to do so invokes
    undefined behavior and you have left the C universe for parts unknown.

    >
    >gk245 wrote:
    >> For instance, if i had a array like this:
    >>
    >> array[] = {1, 2, 3, 5, 6, ......};
    >>
    >> How do i tell C go get me the number of values in the array? Not the
    >> value itself, but how MANY of them there are.
    >>
    >> Thanks, please don't give me anything fancy. Since i can't use other
    >> functions to give that answer, just for loops and such.



    Remove del for email
    Barry Schwarz, Mar 18, 2006
    #17
  18. On Thu, 16 Mar 2006 11:36:22 -0800, Ben Pfaff <>
    wrote:

    > gk245 <> writes:
    >
    > > For instance, if i had a array like this:
    > >
    > > array[] = {1, 2, 3, 5, 6, ......};
    > >
    > > How do i tell C go get me the number of values in the array? Not the
    > > value itself, but how MANY of them there are.

    >
    > sizeof array / sizeof *array
    >
    > (This will work only for an actual array, not for a pointer into
    > an array.)


    Correct, but I think it is worth noting explicitly a consequence of
    this that people (too) often miss:

    And if you (try to) declare function's (formal) parameter as an array,
    it is actually 'adjusted' to a pointer, and this method doesn't work.

    - David.Thompson1 at worldnet.att.net
    Dave Thompson, Mar 27, 2006
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Larry R
    Replies:
    4
    Views:
    885
    Peter Flynn
    Sep 29, 2005
  2. efelnavarro09
    Replies:
    2
    Views:
    911
    efelnavarro09
    Jan 26, 2011
  3. Jean-Sébastien

    count : array to array of hashes

    Jean-Sébastien, Aug 1, 2007, in forum: Ruby
    Replies:
    2
    Views:
    120
    Stefano Crocco
    Aug 1, 2007
  4. Richard Sandoval
    Replies:
    3
    Views:
    104
    Iñaki Baz Castillo
    Apr 16, 2011
  5. Oliver Meister

    Count values in multi dimensional array

    Oliver Meister, Jul 26, 2006, in forum: Perl Misc
    Replies:
    4
    Views:
    150
    Oliver Meister
    Jul 27, 2006
Loading...

Share This Page