size of an array (number of elements)

Discussion in 'C Programming' started by Grey Alien, Jul 4, 2007.

  1. Grey Alien

    Grey Alien Guest

    If I have a struct declared as :

    struct A
    {
    double x ;
    char name[LONG_ENOUGH];
    struct Other other ;
    void * ptr ;
    };


    And I have an array of these structs, how can I determine the number of
    items in the array. I need to be able to determine this since I have a
    function with signature:

    void foo(struct A array_[])
    {
    //Process each of the elements in the passed array
    }
    Grey Alien, Jul 4, 2007
    #1
    1. Advertising

  2. Grey Alien wrote:
    > If I have a struct declared as :
    >
    > struct A
    > {
    > double x ;
    > char name[LONG_ENOUGH];
    > struct Other other ;
    > void * ptr ;
    > };
    >
    >
    > And I have an array of these structs, how can I determine the number of
    > items in the array.


    In general, you can get the size of an array x by calculating
    sizeof x / sizeof *x. However, see below.

    > I need to be able to determine this since I have a
    > function with signature:
    >
    > void foo(struct A array_[])
    > {
    > //Process each of the elements in the passed array
    > }


    Inside foo, you do not have any array of struct A. You have a pointer to
    struct A. From this pointer, it is not possible to reconstruct the size of
    any array it might point to. Either tell it directly, using an extra
    parameter, or put some marker in struct A.
    Harald van =?UTF-8?B?RMSzaw==?=, Jul 4, 2007
    #2
    1. Advertising

  3. In article <>,
    Grey Alien <> wrote:
    >If I have a struct declared as :


    >struct A
    >{
    > double x ;
    > char name[LONG_ENOUGH];
    > struct Other other ;
    > void * ptr ;
    >};



    >And I have an array of these structs, how can I determine the number of
    >items in the array. I need to be able to determine this since I have a
    >function with signature:


    >void foo(struct A array_[])
    >{
    > //Process each of the elements in the passed array
    >}



    You can't do it inside the function; you have to pass the number
    of elements in.

    At the scope that declares the variable, you can use
    sizeof(TheArray) / sizeof(TheArray[0])
    (unless, that is, that TheArray is dynamically allocated storage.)
    --
    All is vanity. -- Ecclesiastes
    Walter Roberson, Jul 4, 2007
    #3
  4. Grey Alien

    Flash Gordon Guest

    Grey Alien wrote, On 04/07/07 19:24:
    > If I have a struct declared as :
    >
    > struct A
    > {
    > double x ;
    > char name[LONG_ENOUGH];
    > struct Other other ;
    > void * ptr ;
    > };
    >
    >
    > And I have an array of these structs, how can I determine the number of
    > items in the array.


    That would be question 6.23 of the comp.lang.c FAQ were you dealing with
    an array, but...

    > I need to be able to determine this since I have a
    > function with signature:
    >
    > void foo(struct A array_[])


    You should read question 6.4 of the comp.lang.c FAQ as well to see why
    you do *not* have an array in here.

    > {
    > //Process each of the elements in the passed array
    > }


    Change the signature to pass in the size of have a sentinal value at the
    end of the array.
    --
    Flash Gordon
    Flash Gordon, Jul 4, 2007
    #4
  5. Grey Alien

    CBFalconer Guest

    Grey Alien wrote:
    >
    > If I have a struct declared as :
    >
    > struct A {
    > double x ;
    > char name[LONG_ENOUGH];
    > struct Other other ;
    > void * ptr ;
    > };
    >
    > And I have an array of these structs, how can I determine the
    > number of items in the array. I need to be able to determine this
    > since I have a function with signature:
    >
    > void foo(struct A array_[]) {
    > //Process each of the elements in the passed array
    > }


    You look closely at the (incomplete) definition of the struct,
    laboriously count the objects identified, and use that value. In
    this case the value would be 4.

    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
    <http://www.securityfocus.com/columnists/423>
    <http://www.aaxnet.com/editor/edit043.html>
    cbfalconer at maineline dot net



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Jul 5, 2007
    #5
  6. Grey Alien

    Flash Gordon Guest

    CBFalconer wrote, On 05/07/07 00:53:
    > Grey Alien wrote:
    >> If I have a struct declared as :
    >>
    >> struct A {
    >> double x ;
    >> char name[LONG_ENOUGH];
    >> struct Other other ;
    >> void * ptr ;
    >> };
    >>
    >> And I have an array of these structs, how can I determine the
    >> number of items in the array. I need to be able to determine this
    >> since I have a function with signature:
    >>
    >> void foo(struct A array_[]) {
    >> //Process each of the elements in the passed array
    >> }

    >
    > You look closely at the (incomplete) definition of the struct,
    > laboriously count the objects identified, and use that value. In
    > this case the value would be 4.


    Chuck, I don't know where you get 4 from (well, I can guess) in the
    above since the OP wants to iterate over the array, not over the fields
    in the struct. Looking at the struct definition does not give you any
    clue about how many elements the array has.
    --
    Flash Gordon
    Flash Gordon, Jul 5, 2007
    #6
  7. CBFalconer <> writes:
    > Grey Alien wrote:
    >> If I have a struct declared as :
    >>
    >> struct A {
    >> double x ;
    >> char name[LONG_ENOUGH];
    >> struct Other other ;
    >> void * ptr ;
    >> };
    >>
    >> And I have an array of these structs, how can I determine the
    >> number of items in the array. I need to be able to determine this
    >> since I have a function with signature:
    >>
    >> void foo(struct A array_[]) {
    >> //Process each of the elements in the passed array
    >> }

    >
    > You look closely at the (incomplete) definition of the struct,
    > laboriously count the objects identified, and use that value. In
    > this case the value would be 4.


    Huh? That's the number of members in the structure. He was clearly
    asking about the number of elements in an array of structures.

    --
    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."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jul 5, 2007
    #7
  8. Grey Alien

    CBFalconer Guest

    Flash Gordon wrote:
    > CBFalconer wrote, On 05/07/07 00:53:
    >> Grey Alien wrote:
    >>
    >>> If I have a struct declared as :
    >>>
    >>> struct A {
    >>> double x ;
    >>> char name[LONG_ENOUGH];
    >>> struct Other other ;
    >>> void * ptr ;
    >>> };
    >>>
    >>> And I have an array of these structs, how can I determine the
    >>> number of items in the array. I need to be able to determine this
    >>> since I have a function with signature:
    >>>
    >>> void foo(struct A array_[]) {
    >>> //Process each of the elements in the passed array
    >>> }

    >>
    >> You look closely at the (incomplete) definition of the struct,
    >> laboriously count the objects identified, and use that value. In
    >> this case the value would be 4.

    >
    > Chuck, I don't know where you get 4 from (well, I can guess) in
    > the above since the OP wants to iterate over the array, not over
    > the fields in the struct. Looking at the struct definition does
    > not give you any clue about how many elements the array has.


    Yes it does. There is one double, one char array, one struct
    (undefined), and one void* pointer. That makes 4 objects in the
    struct to me.

    If he wants to know how big the array of struct A is he will have
    to pass that value in. It is not calculable in the called
    function. That is a separate question.

    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
    <http://www.securityfocus.com/columnists/423>
    <http://www.aaxnet.com/editor/edit043.html>
    cbfalconer at maineline dot net



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Jul 5, 2007
    #8
  9. CBFalconer <> writes:
    > Flash Gordon wrote:
    >> CBFalconer wrote, On 05/07/07 00:53:
    >>> Grey Alien wrote:
    >>>> If I have a struct declared as :
    >>>>
    >>>> struct A {
    >>>> double x ;
    >>>> char name[LONG_ENOUGH];
    >>>> struct Other other ;
    >>>> void * ptr ;
    >>>> };
    >>>>
    >>>> And I have an array of these structs, how can I determine the
    >>>> number of items in the array. I need to be able to determine this
    >>>> since I have a function with signature:
    >>>>
    >>>> void foo(struct A array_[]) {
    >>>> //Process each of the elements in the passed array
    >>>> }
    >>>
    >>> You look closely at the (incomplete) definition of the struct,
    >>> laboriously count the objects identified, and use that value. In
    >>> this case the value would be 4.

    >>
    >> Chuck, I don't know where you get 4 from (well, I can guess) in
    >> the above since the OP wants to iterate over the array, not over
    >> the fields in the struct. Looking at the struct definition does
    >> not give you any clue about how many elements the array has.

    >
    > Yes it does. There is one double, one char array, one struct
    > (undefined), and one void* pointer. That makes 4 objects in the
    > struct to me.
    >
    > If he wants to know how big the array of struct A is he will have
    > to pass that value in. It is not calculable in the called
    > function. That is a separate question.


    Yes, and it's the question he asked. "And I have an array of these
    structs, how can I determine the number of items in the array."

    --
    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."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jul 5, 2007
    #9
  10. Grey Alien

    Flash Gordon Guest

    CBFalconer wrote, On 05/07/07 07:32:
    > Flash Gordon wrote:
    >> CBFalconer wrote, On 05/07/07 00:53:
    >>> Grey Alien wrote:
    >>>
    >>>> If I have a struct declared as :
    >>>>
    >>>> struct A {
    >>>> double x ;
    >>>> char name[LONG_ENOUGH];
    >>>> struct Other other ;
    >>>> void * ptr ;
    >>>> };
    >>>>
    >>>> And I have an array of these structs, how can I determine the
    >>>> number of items in the array. I need to be able to determine this

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >>>> since I have a function with signature:
    >>>>
    >>>> void foo(struct A array_[]) {
    >>>> //Process each of the elements in the passed array

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >>>> }
    >>> You look closely at the (incomplete) definition of the struct,
    >>> laboriously count the objects identified, and use that value. In
    >>> this case the value would be 4.

    >> Chuck, I don't know where you get 4 from (well, I can guess) in
    >> the above since the OP wants to iterate over the array, not over

    ^^^^^^^^^^^^^^^^^^^^^^
    >> the fields in the struct. Looking at the struct definition does
    >> not give you any clue about how many elements the array has.

    >
    > Yes it does. There is one double, one char array, one struct
    > (undefined), and one void* pointer. That makes 4 objects in the
    > struct to me.


    That is how many fields the *struct* has, NOT how many elements the
    array has, which is what the OP asked. What makes you think iterating
    over the array, as the OP stated was the requirement, and as I stated
    above was the requirement, has anything to do with the number of fields
    in the struct?

    > If he wants to know how big the array of struct A is he will have
    > to pass that value in. It is not calculable in the called
    > function. That is a separate question.


    It is the question asked and answered by various others. You seem to be
    the only person reading it as a request for the number of fields, so I
    suggest it is you expecting the OP to be asking the wrong question and
    reading what you expect, not what is there.

    Reread the underlined bits.
    --
    Flash Gordon
    Flash Gordon, Jul 5, 2007
    #10
    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. P
    Replies:
    1
    Views:
    1,162
    Joe Kesselman
    Jul 7, 2006
  2. Herrcho
    Replies:
    6
    Views:
    449
    Peter Pichler
    Feb 20, 2004
  3. Replies:
    4
    Views:
    947
    Martin Ambuhl
    May 20, 2005
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,568
    Michael Jung
    May 25, 2008
  5. Hicham Mouline
    Replies:
    1
    Views:
    392
    Kai-Uwe Bux
    Apr 11, 2010
Loading...

Share This Page