array size

Discussion in 'C Programming' started by RedLars, Sep 24, 2007.

  1. RedLars

    RedLars Guest

    Given this generic object;
    typedef struct
    {
    const char * name;
    int address;

    } Parameter;

    I defined the following array of Parameters;
    static Parameter para = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

    Would this be the most robust \ safest way of getting the size of the
    array ?
    void foo(Parameter * parameter)
    {
    int arraySize = 0;
    while (parameter->name != NULL) arraySize++;

    }

    Thanks
     
    RedLars, Sep 24, 2007
    #1
    1. Advertising

  2. RedLars wrote:

    > {
    > int arraySize = 0;
    > while (parameter->name != NULL) arraySize++;
    >
    > }
    >

    This will return infinity or zero (if parameter was NULL)
    look at this code:
    {
    int arraySize = 0; /* array size in bytes */
    for(;parameter->name != NULL; parameter++) {
    arraySize += sizeof(Parameter);
    }
    --
    s/.../.gotovchits/g for email.
     
    Ivan Gotovchits, Sep 24, 2007
    #2
    1. Advertising

  3. Ivan Gotovchits wrote:

    > {
    > int arraySize = 0; /* array size in bytes */
    > for(;parameter->name != NULL; parameter++) {
    > arraySize += sizeof(Parameter);
    > }

    }
    forgotten ))
    --
    s/.../.gotovchits/g for email.
     
    Ivan Gotovchits, Sep 24, 2007
    #3
  4. On 2007-09-24, RedLars <> wrote:
    > Given this generic object;
    > typedef struct
    > {
    > const char * name;
    > int address;
    >
    > } Parameter;
    >
    > I defined the following array of Parameters;
    > static Parameter para = {{"BUS", 1},{"Network", 102}, {NULL, 0}};
    >
    > Would this be the most robust \ safest way of getting the size of the
    > array ?
    > void foo(Parameter * parameter)
    > {
    > int arraySize = 0;
    > while (parameter->name != NULL) arraySize++;
    >
    > }
    >
    > Thanks
    >


    Why not sizeof(para)/sizeof(Parameter) since your array is fixed ?

    Gilles

    --
    Booze is the answer. I don't remember the question.
     
    Gilles Chehade, Sep 24, 2007
    #4
  5. On 2007-09-24, Gilles Chehade <> wrote:
    > On 2007-09-24, RedLars <> wrote:
    >> Given this generic object;
    >> typedef struct
    >> {
    >> const char * name;
    >> int address;
    >>
    >> } Parameter;
    >>
    >> I defined the following array of Parameters;
    >> static Parameter para = {{"BUS", 1},{"Network", 102}, {NULL, 0}};
    >>
    >> Would this be the most robust \ safest way of getting the size of the
    >> array ?
    >> void foo(Parameter * parameter)
    >> {
    >> int arraySize = 0;
    >> while (parameter->name != NULL) arraySize++;
    >>
    >> }
    >>
    >> Thanks
    >>

    >
    > Why not sizeof(para)/sizeof(Parameter) since your array is fixed ?
    >


    Sorry I misread your declaration, this would be:
    sizeof(para)/sizeof(Parameter) - 1;

    --
    Booze is the answer. I don't remember the question.
     
    Gilles Chehade, Sep 24, 2007
    #5
  6. RedLars

    Mark Bluemel Guest

    RedLars wrote:
    > Given this generic object;
    > typedef struct
    > {
    > const char * name;
    > int address;
    >
    > } Parameter;
    >
    > I defined the following array of Parameters;
    > static Parameter para = {{"BUS", 1},{"Network", 102}, {NULL, 0}};


    I don't think you did. That won't compile...

    You should really use copy and paste to show us your source.

    The code probably reads :-

    static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

    >
    > Would this be the most robust \ safest way of getting the size of the
    > array ?



    What's wrong with this?

    sizeof para/sizeof(Parameter)
     
    Mark Bluemel, Sep 24, 2007
    #6
  7. RedLars

    RedLars Guest

    Mark: Thank you for pointing out a flaw in the code;
    static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

    (Thats what happens when I try to type something based on memory)

    Sorry for not explaining myself properly, using
    sizeof para/sizeof(Parameter)

    would give me the size of the variable para which is fine. However, I
    would like to get the size of Parameter * passed to the function foo
    (the definition of para is unknown to this function).

    void foo(Parameter * parameter)
    {

    }

    Appreciate any help.

    On 24 Sep, 16:54, Mark Bluemel <> wrote:
    > RedLars wrote:
    > > Given this generic object;
    > > typedef struct
    > > {
    > > const char * name;
    > > int address;

    >
    > > } Parameter;

    >
    > > I defined the following array of Parameters;
    > > static Parameter para = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

    >
    > I don't think you did. That won't compile...
    >
    > You should really use copy and paste to show us your source.
    >
    > The code probably reads :-
    >
    > static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};
    >
    >
    >
    > > Would this be the most robust \ safest way of getting the size of the
    > > array ?

    >
    > What's wrong with this?
    >
    > sizeof para/sizeof(Parameter)
     
    RedLars, Sep 24, 2007
    #7
  8. RedLars

    Default User Guest

    Re: array size - TPA

    RedLars wrote:

    > Mark: Thank you for pointing out a flaw in the code;



    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or:
    <http://www.caliburn.nl/topposting.html>
     
    Default User, Sep 24, 2007
    #8
  9. RedLars

    Army1987 Guest

    On Mon, 24 Sep 2007 11:36:18 -0700, RedLars wrote:

    > Sorry for not explaining myself properly, using
    > sizeof para/sizeof(Parameter)
    >
    > would give me the size of the variable para which is fine. However, I
    > would like to get the size of Parameter * passed to the function foo
    > (the definition of para is unknown to this function).
    >
    > void foo(Parameter * parameter)
    > {
    >
    > }

    You can't. Simply pass the number of Parameter's as an argument:
    void foo(Parameter *parameter, size_t n_of_pars);
    --
    Army1987 (Replace "NOSPAM" with "email")
    A hamburger is better than nothing.
    Nothing is better than eternal happiness.
    Therefore, a hamburger is better than eternal happiness.
     
    Army1987, Sep 24, 2007
    #9
  10. RedLars

    Tor Rustad Guest

    RedLars wrote:

    [...]

    > Would this be the most robust \ safest way of getting the size of the
    > array ?


    I like this macro:

    #define ARRAY_SIZE(x) (sizeof (x) / sizeof (x[0]))

    > void foo(Parameter * parameter)


    void foo(Parameter *param, size_t max_param)


    --
    Tor <torust [at] online [dot] no>
     
    Tor Rustad, Sep 24, 2007
    #10
  11. RedLars

    pete Guest

    RedLars wrote:
    >
    > Mark: Thank you for pointing out a flaw in the code;
    > static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};
    >
    > (Thats what happens when I try to type something based on memory)
    >
    > Sorry for not explaining myself properly, using
    > sizeof para/sizeof(Parameter)
    >
    > would give me the size of the variable para which is fine. However, I
    > would like to get the size of Parameter * passed to the function foo
    > (the definition of para is unknown to this function).
    >
    > void foo(Parameter * parameter)
    > {
    >
    > }
    >
    > Appreciate any help.
    >
    > On 24 Sep, 16:54, Mark Bluemel <> wrote:
    > > RedLars wrote:
    > > > Given this generic object;
    > > > typedef struct
    > > > {
    > > > const char * name;
    > > > int address;

    > >
    > > > } Parameter;

    > >
    > > > I defined the following array of Parameters;
    > > > static Parameter para = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

    > >
    > > I don't think you did. That won't compile...
    > >
    > > You should really use copy and paste to show us your source.
    > >
    > > The code probably reads :-
    > >
    > > static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};
    > >
    > >
    > >
    > > > Would this be the most robust \
    > > > safest way of getting the size of the array ?

    > >
    > > What's wrong with this?
    > >
    > > sizeof para/sizeof(Parameter)


    /* BEGIN new.c */

    #include <stdio.h>

    typedef struct {
    const char * name;
    int address;
    } Parameter;

    void foo(Parameter * parameter, size_t nmemb);

    int main(void)
    {
    static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

    foo(para, sizeof para / sizeof *para);
    return 0;
    }

    void foo(Parameter * parameter, size_t nmemb)
    {
    size_t index;

    printf("The Parameter array has %u elements.\n", (unsigned)nmemb);
    for (index = 0; index != nmemb; ++index) {
    if (parameter[index].name != NULL) {
    printf("%s ", parameter[index].name);
    } else {
    printf("NULL ");
    }
    printf("%d\n", parameter[index].address);
    }
    }
    /* END new.c */

    --
    pete
     
    pete, Sep 24, 2007
    #11
  12. On Mon, 24 Sep 2007 11:36:18 -0700, RedLars <>
    wrote:

    >Mark: Thank you for pointing out a flaw in the code;
    >static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};
    >
    >(Thats what happens when I try to type something based on memory)
    >
    >Sorry for not explaining myself properly, using
    >sizeof para/sizeof(Parameter)
    >
    >would give me the size of the variable para which is fine. However, I
    >would like to get the size of Parameter * passed to the function foo
    >(the definition of para is unknown to this function).
    >
    >void foo(Parameter * parameter)
    >{
    >
    >}
    >


    snip

    Please don't top post. Place your response immediately after the
    relevant original material you are responding to.

    At the time foo is executing, parameter contains no information about
    the size of the array para. In fact, it points to para[0] and there
    need not be a para[1]. The recommended approach is to add a second
    parameter to foo along the lines of
    size_t number_of_elements_in_array

    In the calling statement you could then pass the argument using
    sizeof para / sizeof *para
    since in that function para is a defined array.

    If para were an allocated array, the above argument would not work.
    You would have to keep track of how much space you allocated and pass
    that value directly.


    Remove del for email
     
    Barry Schwarz, Sep 25, 2007
    #12
  13. RedLars

    RedLars Guest

    On 25 Sep, 02:47, Barry Schwarz <> wrote:
    > On Mon, 24 Sep 2007 11:36:18 -0700, RedLars <>
    > wrote:
    > Please don't top post. Place your response immediately after the
    > relevant original material you are responding to.


    Sorry about that.

    > At the time foo is executing, parameter contains no information about
    > the size of the array para. In fact, it points to para[0] and there
    > need not be a para[1]. The recommended approach is to add a second
    > parameter to foo along the lines of
    > size_t number_of_elements_in_array
    >

    So it's not possible to add sort of a dummy object at the end of the
    array. Then inside foo(..) one could loop through the array, looking
    for the dummy instance.

    static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};

    Just a thought..
     
    RedLars, Sep 25, 2007
    #13
  14. "RedLars" <> schrieb im Newsbeitrag
    news:...
    > On 25 Sep, 02:47, Barry Schwarz <> wrote:
    >> On Mon, 24 Sep 2007 11:36:18 -0700, RedLars <>
    >> wrote:
    >> Please don't top post. Place your response immediately after the
    >> relevant original material you are responding to.

    >
    > Sorry about that.
    >
    >> At the time foo is executing, parameter contains no information about
    >> the size of the array para. In fact, it points to para[0] and there
    >> need not be a para[1]. The recommended approach is to add a second
    >> parameter to foo along the lines of
    >> size_t number_of_elements_in_array
    >>

    > So it's not possible to add sort of a dummy object at the end of the
    > array. Then inside foo(..) one could loop through the array, looking
    > for the dummy instance.
    >
    > static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};
    >
    > Just a thought..

    This is indeed possible, and there are library functions that do exactly
    this. It requires one more element in the array though (the end marker) and
    also needs some code in the receiving function to count.

    Bye, Jojo
     
    Joachim Schmitz, Sep 25, 2007
    #14
  15. RedLars <> writes:
    > On 25 Sep, 02:47, Barry Schwarz <> wrote:
    >> On Mon, 24 Sep 2007 11:36:18 -0700, RedLars <>
    >> wrote:
    >> Please don't top post. Place your response immediately after the
    >> relevant original material you are responding to.

    >
    > Sorry about that.
    >
    >> At the time foo is executing, parameter contains no information about
    >> the size of the array para. In fact, it points to para[0] and there
    >> need not be a para[1]. The recommended approach is to add a second
    >> parameter to foo along the lines of
    >> size_t number_of_elements_in_array
    >>

    > So it's not possible to add sort of a dummy object at the end of the
    > array. Then inside foo(..) one could loop through the array, looking
    > for the dummy instance.
    >
    > static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};
    >
    > Just a thought..


    Sure, you can do that. C strings use the same technique, using '\0'
    to mark the end.

    You can't directly pass arrays as parameters, so instead you pass a
    pointer to the first element, which of course loses information about
    the length. There are a number of ways to get that information into
    the function. The most straightforward is to pass it as a separate
    argument, but using a special terminator works too -- assuming the
    terminator can't be a valid value. But it means you need to scan the
    array to find the terminator, which may or may not cause performance
    problems.

    --
    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, Sep 25, 2007
    #15
  16. On Mon, 24 Sep 2007 22:59:09 -0700, RedLars <>
    wrote:

    >On 25 Sep, 02:47, Barry Schwarz <> wrote:
    >> On Mon, 24 Sep 2007 11:36:18 -0700, RedLars <>
    >> wrote:
    >> Please don't top post. Place your response immediately after the
    >> relevant original material you are responding to.

    >
    >Sorry about that.
    >
    >> At the time foo is executing, parameter contains no information about
    >> the size of the array para. In fact, it points to para[0] and there
    >> need not be a para[1]. The recommended approach is to add a second
    >> parameter to foo along the lines of
    >> size_t number_of_elements_in_array
    >>

    >So it's not possible to add sort of a dummy object at the end of the
    >array. Then inside foo(..) one could loop through the array, looking
    >for the dummy instance.
    >
    >static Parameter para[] = {{"BUS", 1},{"Network", 102}, {NULL, 0}};
    >
    >Just a thought..


    This will work if there is never any situation where the (first)
    member of your structure could have the value NULL legitimately. In
    the case where the member is a pointer, this is often reasonable. But
    what about the case where the members are all arithmetic values?
    Unless you know that some member value is never legitimate (such as 0
    or INT_MIN), it is hard to determine if the structure is really a
    sentinel or just an array element with "strange" values.


    Remove del for email
     
    Barry Schwarz, Sep 26, 2007
    #16
    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. Zygmunt Krynicki
    Replies:
    1
    Views:
    643
    Ivan Vecerina
    Oct 11, 2003
  2. Denis Palas
    Replies:
    2
    Views:
    728
    Daniel Pitts
    Dec 19, 2006
  3. Replies:
    5
    Views:
    584
    Flash Gordon
    Apr 9, 2006
  4. Replies:
    8
    Views:
    531
    Bob Hairgrove
    Apr 10, 2006
  5. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,742
    Michael Jung
    May 25, 2008
Loading...

Share This Page