2 stupid array questions!!

Discussion in 'C Programming' started by prashna, Oct 10, 2003.

  1. prashna

    prashna Guest

    Hi Guru's,
    Here are my questions...

    1)Why does c allows an extra "," in array intialiser?Is there any
    advantage of this?

    ex: int arr[5]={1,2,3,4,5,};
    ^^Compiler does not give error for this!

    2)How to determine the size of the array which is passes as a
    parameter to the function?
    ex :
    void foo(int arr[])
    {
    /*I want determine the size of the array "arr" in this function.I
    tried *using "sizeof" operator and realised it is not going to work
    as "arr" will be *treated as pointer to an int!!!!!!*/
    }

    Thanks in advance
     
    prashna, Oct 10, 2003
    #1
    1. Advertising

  2. "prashna" <> wrote in message
    news:...
    | 1)Why does c allows an extra "," in array intialiser?Is there any
    | advantage of this?
    |
    | ex: int arr[5]={1,2,3,4,5,};
    | ^^Compiler does not give error for this!
    It makes things easier for code generators.
    No real benefit besides that...

    | 2)How to determine the size of the array which is passes as a
    | parameter to the function?
    | ex :
    | void foo(int arr[])
    | {
    | /*I want determine the size of the array "arr" in this function.I
    | tried *using "sizeof" operator and realised it is not going to work
    | as "arr" will be *treated as pointer to an int!!!!!!*/
    | }
    The size of the array needs to be passed as a second parameter.
    No other way in C (C++ allows what you need, through templates...).

    hth,
    Ivan
    --
    http://ivan.vecerina.com
     
    Ivan Vecerina, Oct 10, 2003
    #2
    1. Advertising

  3. prashna

    pete Guest

    prashna wrote:
    >
    > Hi Guru's,
    > Here are my questions...
    >
    > 1)Why does c allows an extra "," in array intialiser?Is there any
    > advantage of this?
    >
    > ex: int arr[5]={1,2,3,4,5,};
    > ^^Compiler does not give error for this!
    >
    > 2)How to determine the size of the array which is passes as a
    > parameter to the function?
    > ex :
    > void foo(int arr[])
    > {
    > /*I want determine the size of the array "arr" in this function.I
    > tried *using "sizeof" operator and realised it is not going to work
    > as "arr" will be *treated as pointer to an int!!!!!!*/
    > }


    If you want foo to know the size,
    then you have to give foo more information.

    void foo(int arr[], size_t n_elem) /* number of elements */

    void foo(int arr[], size_t bytes) /* size in bytes */

    --
    pete
     
    pete, Oct 10, 2003
    #3
  4. In article <>, prashna wrote:
    > Hi Guru's,
    > Here are my questions...
    >
    > 1)Why does c allows an extra "," in array intialiser?Is there any
    > advantage of this?
    >
    > ex: int arr[5]={1,2,3,4,5,};
    > ^^Compiler does not give error for this!


    It simplifies code that generate C code.

    Oh, and the 5 in [5] is not needed in your example.

    >
    > 2)How to determine the size of the array which is passes as a
    > parameter to the function?

    [cut]


    Change the interface of the function so that the caller also
    sends the length of the array (i.e. add an extra int argument).


    --
    Andreas Kähäri
     
    Andreas Kahari, Oct 10, 2003
    #4
  5. [OT] Re: 2 stupid array questions!!

    On Fri, 10 Oct 2003 11:48:00 +0200, Ivan Vecerina wrote:

    > | 2)How to determine the size of the array which is passes as a
    > | parameter to the function?
    > | ex :
    > | void foo(int arr[])
    > | {
    > | /*I want determine the size of the array "arr" in this function.I
    > | tried *using "sizeof" operator and realised it is not going to work
    > | as "arr" will be *treated as pointer to an int!!!!!!*/
    > | }
    > The size of the array needs to be passed as a second parameter.
    > No other way in C (C++ allows what you need, through templates...).


    Offtopic since C++
    I must disagree. Usage of a template in this case would be a total
    overkill. I believe vector and other collections address this problem
    better.

    Regards
    Zygmunt Krynicki
     
    Zygmunt Krynicki, Oct 10, 2003
    #5
  6. prashna

    Default User Guest

    Re: [OT] Re: 2 stupid array questions!!

    Zygmunt Krynicki wrote:

    > Offtopic since C++
    > I must disagree. Usage of a template in this case would be a total
    > overkill. I believe vector and other collections address this problem
    > better.



    Of course, that just hides the templates from the user. They're still
    there.




    Brian Rodenborn
     
    Default User, Oct 10, 2003
    #6
  7. [OT] Re: 2 stupid array questions!!

    "Zygmunt Krynicki" <zyga@_CUT_2zyga.MEdyndns._OUT_org> wrote in message
    news:pan.2003.10.10.19.03.41.271708@_CUT_2zyga.MEdyndns._OUT_org...
    > On Fri, 10 Oct 2003 11:48:00 +0200, Ivan Vecerina wrote:
    >
    > > | 2)How to determine the size of the array which is passes as a
    > > | parameter to the function?
    > > | ex :
    > > | void foo(int arr[])
    > > | {
    > > | /*I want determine the size of the array "arr" in this function.I
    > > | tried *using "sizeof" operator and realised it is not going to work
    > > | as "arr" will be *treated as pointer to an int!!!!!!*/
    > > | }
    > > The size of the array needs to be passed as a second parameter.
    > > No other way in C (C++ allows what you need, through templates...).

    >
    > Offtopic since C++
    > I must disagree. Usage of a template in this case would be a total
    > overkill. I believe vector and other collections address this problem
    > better.


    What exactly is overkill in the following examples ?

    // returns the size of the C array passed as a param
    // This is safer than the sizeof(a)/sizeof(a[0]) trick...
    template <typename T, int N> inline
    int arraySize(T (&)[N]) { return N; }

    // fills an array with a specified value
    template<typename T, int N> inline
    void fill( T (&array)[N], T const& value )
    { for(int i=0;i<N;++i) array=value; }

    If code duplication is of concern (in the second case), you could
    choose to call a back-end function that takes a pointer and
    an array size as parameters -- and benefit from the template still.


    Regards,
    Ivan
    --
    http://ivan.vecerina.com
     
    Ivan Vecerina, Oct 10, 2003
    #7
  8. Re: [OT] Re: 2 stupid array questions!!

    On Fri, 10 Oct 2003 21:56:55 +0200, Ivan Vecerina wrote:

    > What exactly is overkill in the following examples ?
    >
    > // returns the size of the C array passed as a param
    > // This is safer than the sizeof(a)/sizeof(a[0]) trick...
    > template <typename T, int N> inline
    > int arraySize(T (&)[N]) { return N; }


    This can only be used in the scope of the declaration of the array you're
    going to pass along.

    > // fills an array with a specified value
    > template<typename T, int N> inline
    > void fill( T (&array)[N], T const& value )
    > { for(int i=0;i<N;++i) array=value; }


    Don't reinvent the wheel: std::fill

    > If code duplication is of concern (in the second case), you could
    > choose to call a back-end function that takes a pointer and
    > an array size as parameters -- and benefit from the template still.


    What is the benefit?
    That I can type foo(a) instead of foo(a, n)?
    I don't see that as a benefit but a inconveniance. It's illogical to
    assume all routines need to operate on the whole set of data.
    This is why most of STL routines use begin/end iterators as input.

    Your code, interesting in its design, is not the kind of a solution
    I would advice to anyone interested in C++, usage of plain C arrays
    is a bad thing (to quote the appropriate FAQ) as it undermines your
    code with all C-memory-handling related issues.

    Regards
    Zygmunt Krynicki
     
    Zygmunt Krynicki, Oct 10, 2003
    #8
  9. Re: [OT] Re: 2 stupid array questions!!

    On Fri, 10 Oct 2003 19:51:08 +0000, Default User wrote:

    > Zygmunt Krynicki wrote:
    >
    >> Offtopic since C++
    >> I must disagree. Usage of a template in this case would be a total
    >> overkill. I believe vector and other collections address this problem
    >> better.

    >
    >
    > Of course, that just hides the templates from the user. They're still
    > there.


    I was not trying to say that templates are not present or that they are
    bad in general. I was just objecting to using them in accompaniance with C
    arrays as a method of obtaining the declared array size.

    Regards

    Zygmunt
     
    Zygmunt Krynicki, Oct 10, 2003
    #9
  10. prashna

    Default User Guest

    Re: [OT] Re: 2 stupid array questions!!

    Zygmunt Krynicki wrote:

    > Don't reinvent the wheel: std::fill


    Did you forget which newsgroup you are on?


    > Your code, interesting in its design, is not the kind of a solution
    > I would advice to anyone interested in C++, usage of plain C arrays
    > is a bad thing (to quote the appropriate FAQ) as it undermines your
    > code with all C-memory-handling related issues.



    Seems like it. This is comp.lang.c.



    Brian Rodenborn
     
    Default User, Oct 10, 2003
    #10
  11. Re: [OT] Re: 2 stupid array questions!!

    "Zygmunt Krynicki" <zyga@_CUT_2zyga.MEdyndns._OUT_org> wrote in message
    news:pan.2003.10.10.20.24.28.829682@_CUT_2zyga.MEdyndns._OUT_org...
    > On Fri, 10 Oct 2003 21:56:55 +0200, Ivan Vecerina wrote:
    > > What exactly is overkill in the following examples ?
    > >
    > > // returns the size of the C array passed as a param
    > > // This is safer than the sizeof(a)/sizeof(a[0]) trick...
    > > template <typename T, int N> inline
    > > int arraySize(T (&)[N]) { return N; }

    >
    > This can only be used in the scope of the declaration of the array you're
    > going to pass along.


    Yes. And it will produce a compile error when mis-used.
    As stated in my comment, this is much safer than the classic sizeof trick.

    > > // fills an array with a specified value
    > > template<typename T, int N> inline
    > > void fill( T (&array)[N], T const& value )
    > > { for(int i=0;i<N;++i) array=value; }

    >
    > Don't reinvent the wheel: std::fill
    >
    > > If code duplication is of concern (in the second case), you could
    > > choose to call a back-end function that takes a pointer and
    > > an array size as parameters -- and benefit from the template still.

    >
    > What is the benefit?
    > That I can type foo(a) instead of foo(a, n)?


    When the size of an array is implicitly specified by an initializer-
    list, n is not always known to the reader. So yes, I consider that
    one can benefit from the use of such templates.
    This is why this trick has often been mentioned in the literature,
    and in newsgroups.

    > I don't see that as a benefit but a inconveniance. It's illogical to
    > assume all routines need to operate on the whole set of data.
    > This is why most of STL routines use begin/end iterators as input.


    Even when using the STL, statically initialized arrays are useful,
    be it just to initialize a C++ container from them.
    I could have as well provided the following example:

    // returns a pointer to the end of an array.
    template <typename T, int N>
    T* arrayEnd(T (&a)[N]) { return a+N; }

    > Your code, interesting in its design, is not the kind of a solution
    > I would advice to anyone interested in C++, usage of plain C arrays
    > is a bad thing (to quote the appropriate FAQ) as it undermines your
    > code with all C-memory-handling related issues.


    There is no such thing as a one true way to code in C++.

    Let me get back to your original comment: you said that the usage
    of templates would be overkill. Why ?
    If they provide a safer alternative to C macro tricks,
    why should one avoid them ?

    When I do some embedded programming, I feel my code can truly
    benefit from some C++ techniques, including those that rely on
    templates, even when standard library containers are not only
    overkill, but totally of the question (i.e. w/ only 4Kb of RAM).


    Kind regards,
    Ivan
    --
    http://ivan.vecerina.com
     
    Ivan Vecerina, Oct 10, 2003
    #11
  12. prashna

    Alan Balmer Guest

    Re: [OT] Re: 2 stupid array questions!!

    On Fri, 10 Oct 2003 23:23:45 +0200, "Ivan Vecerina"
    <ivecATmyrealboxDOTcom> wrote:

    >> > // returns the size of the C array passed as a param
    >> > // This is safer than the sizeof(a)/sizeof(a[0]) trick...
    >> > template <typename T, int N> inline
    >> > int arraySize(T (&)[N]) { return N; }

    >>
    >> This can only be used in the scope of the declaration of the array you're
    >> going to pass along.

    >
    >Yes. And it will produce a compile error when mis-used.
    >As stated in my comment, this is much safer than the classic sizeof trick.


    It *always* produces a compile-time error in C.

    *Please* move this thread to a C++ group.

    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, Oct 10, 2003
    #12
  13. prashna

    Jack Klein Guest

    Re: [OT] Re: 2 stupid array questions!!

    On Fri, 10 Oct 2003 21:56:55 +0200, "Ivan Vecerina"
    <ivecATmyrealboxDOTcom> wrote in comp.lang.c:

    > "Zygmunt Krynicki" <zyga@_CUT_2zyga.MEdyndns._OUT_org> wrote in message
    > news:pan.2003.10.10.19.03.41.271708@_CUT_2zyga.MEdyndns._OUT_org...
    > > On Fri, 10 Oct 2003 11:48:00 +0200, Ivan Vecerina wrote:
    > >
    > > > | 2)How to determine the size of the array which is passes as a
    > > > | parameter to the function?
    > > > | ex :
    > > > | void foo(int arr[])
    > > > | {
    > > > | /*I want determine the size of the array "arr" in this function.I
    > > > | tried *using "sizeof" operator and realised it is not going to work
    > > > | as "arr" will be *treated as pointer to an int!!!!!!*/
    > > > | }
    > > > The size of the array needs to be passed as a second parameter.
    > > > No other way in C (C++ allows what you need, through templates...).

    > >
    > > Offtopic since C++
    > > I must disagree. Usage of a template in this case would be a total
    > > overkill. I believe vector and other collections address this problem
    > > better.

    >
    > What exactly is overkill in the following examples ?


    What is WRONG with your code is that this is comp.lang.c, and your
    code is not C.

    Either take it to email or to comp.lang.c++. C++ is OFF-TOPIC here.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
     
    Jack Klein, Oct 11, 2003
    #13
    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. Child
    Replies:
    1
    Views:
    308
    Child
    Aug 8, 2003
  2. Zygmunt Krynicki
    Replies:
    1
    Views:
    643
    Ivan Vecerina
    Oct 11, 2003
  3. Brandon McCombs
    Replies:
    4
    Views:
    535
    Richard Wheeldon
    Aug 28, 2006
  4. Replies:
    3
    Views:
    292
    Jack Klein
    May 28, 2006
  5. rincewind

    stupid, STUPID question!

    rincewind, Apr 19, 2009, in forum: HTML
    Replies:
    25
    Views:
    1,060
Loading...

Share This Page