Variable length arrays Q

Discussion in 'C Programming' started by mechanicfem@googlemail.com, Feb 20, 2006.

  1. Guest

    In f(), I am passing a parameter of array type - /size/ indicates the
    array's length:

    void f(char arr[], int size);

    In the c99 stds, it mentions the use of [*] and says (I think) that it
    can be used to indicate that an array parameter has /variable length/ -
    so, f() may be rewitten as:

    void f(char arr[*], int size);

    So, I think my understanding of [*] is wrong, as I cannot see that this
    adds anything. Unless it's maybe a commenting mnemonic of some type -
    to inform the programmer that arr's length isn't fixed. However, in
    that case, one wonders why the presence of /size/ doesn't do the trick?

    Could someone please tell me what [*] is for then?

    x

    Jo
     
    , Feb 20, 2006
    #1
    1. Advertising

  2. pemo Guest

    wrote:
    > In f(), I am passing a parameter of array type - /size/ indicates the
    > array's length:
    >
    > void f(char arr[], int size);
    >
    > In the c99 stds, it mentions the use of [*] and says (I think) that it
    > can be used to indicate that an array parameter has /variable length/
    > - so, f() may be rewitten as:
    >
    > void f(char arr[*], int size);
    >
    > So, I think my understanding of [*] is wrong, as I cannot see that
    > this adds anything. Unless it's maybe a commenting mnemonic of some
    > type - to inform the programmer that arr's length isn't fixed.
    > However, in that case, one wonders why the presence of /size/ doesn't
    > do the trick?
    >
    > Could someone please tell me what [*] is for then?
    >
    > x
    >
    > Jo


    In 'C - A Reference Manual' (Steele/Harbison) is says on (pg 99) that

    'The * can only appear in array parameter declarations within function
    prototypes that are not part of a function definition'

    So, I assume that this means something like this [although I'm far from
    sure!]:

    void someFunc(char [*], int);

    or

    void someFunc(char arr[*], int size);

    void someFunc(char arr[] int size)
    {
    ...
    }

    It also adds - from various places:

    A VLA *must* be declared at block scope, and cannot be initialised, extern
    or static.

    Also - it says [pg 145 - using VLAs in parameters] 'When an array's length
    is also a parameter, it must necessarily appear first due to C's lexical
    scoping rules'.

    So, presumably, my earlier attempt should be:

    void someFunc(int, char [*]);

    or

    void someFunc(int size, char arr[*]);

    void someFunc(int size, char arr[])
    {
    ...
    }


    --
    ==============
    Not a pedant
    ==============
     
    pemo, Feb 20, 2006
    #2
    1. Advertising

  3. pemo Guest

    pemo wrote:
    > wrote:
    >> In f(), I am passing a parameter of array type - /size/ indicates the
    >> array's length:
    >>
    >> void f(char arr[], int size);
    >>
    >> In the c99 stds, it mentions the use of [*] and says (I think) that
    >> it can be used to indicate that an array parameter has /variable
    >> length/ - so, f() may be rewitten as:
    >>
    >> void f(char arr[*], int size);
    >>
    >> So, I think my understanding of [*] is wrong, as I cannot see that
    >> this adds anything. Unless it's maybe a commenting mnemonic of some
    >> type - to inform the programmer that arr's length isn't fixed.
    >> However, in that case, one wonders why the presence of /size/ doesn't
    >> do the trick?
    >>
    >> Could someone please tell me what [*] is for then?
    >>
    >> x
    >>
    >> Jo

    >
    > In 'C - A Reference Manual' (Steele/Harbison) is says on (pg 99) that
    >
    > 'The * can only appear in array parameter declarations within function
    > prototypes that are not part of a function definition'
    >
    > So, I assume that this means something like this [although I'm far
    > from sure!]:
    >
    > void someFunc(char [*], int);
    >
    > or
    >
    > void someFunc(char arr[*], int size);
    >
    > void someFunc(char arr[] int size)
    > {
    > ...
    > }
    >
    > It also adds - from various places:
    >
    > A VLA *must* be declared at block scope, and cannot be initialised,
    > extern or static.
    >
    > Also - it says [pg 145 - using VLAs in parameters] 'When an array's
    > length is also a parameter, it must necessarily appear first due to
    > C's lexical scoping rules'.
    >
    > So, presumably, my earlier attempt should be:
    >
    > void someFunc(int, char [*]);
    >
    > or
    >
    > void someFunc(int size, char arr[*]);
    >
    > void someFunc(int size, char arr[])
    > {
    > ...
    > }




    >Unless it's maybe a commenting mnemonic of some ...



    Using gcc, this compiles fine...

    void f(char arr[]);

    void f(char arr[])
    {
    ...
    }

    Whilst this gives a warning about gcc not properly supporting variable
    length arrays.

    void f(char arr[*]);

    void f(char arr[])
    {
    ...
    }


    My conclusion from this is that [*] used in a prototype *is* some pseudo
    commenting thingmy?


    --
    ==============
    Not a pedant
    ==============
     
    pemo, Feb 20, 2006
    #3
  4. Michael Mair Guest

    schrieb:
    > In f(), I am passing a parameter of array type - /size/ indicates the
    > array's length:
    >
    > void f(char arr[], int size);
    >
    > In the c99 stds, it mentions the use of [*] and says (I think) that it
    > can be used to indicate that an array parameter has /variable length/ -
    > so, f() may be rewitten as:
    >
    > void f(char arr[*], int size);
    >
    > So, I think my understanding of [*] is wrong, as I cannot see that this
    > adds anything. Unless it's maybe a commenting mnemonic of some type -
    > to inform the programmer that arr's length isn't fixed. However, in
    > that case, one wonders why the presence of /size/ doesn't do the trick?
    >
    > Could someone please tell me what [*] is for then?


    With C99 VLAs, you can do

    void foo (int size, char arr[size])
    {
    ....
    }

    In a prototype of foo,
    void foo (int, char [*]);
    void foo (int size, char arr[*]);
    void foo (int size, char arr[size]);
    are essentially equivalent. The asterisk can only be used in
    function prototypes.
    char [*] is a VLA of (yet) unknown number of elements.
    char [] is just a pointer to char in a function prototype.


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Feb 20, 2006
    #4
  5. pemo Guest

    Michael Mair wrote:
    > schrieb:
    >> In f(), I am passing a parameter of array type - /size/ indicates the
    >> array's length:
    >>
    >> void f(char arr[], int size);
    >>
    >> In the c99 stds, it mentions the use of [*] and says (I think) that
    >> it can be used to indicate that an array parameter has /variable
    >> length/ - so, f() may be rewitten as:
    >>
    >> void f(char arr[*], int size);
    >>
    >> So, I think my understanding of [*] is wrong, as I cannot see that
    >> this adds anything. Unless it's maybe a commenting mnemonic of some
    >> type - to inform the programmer that arr's length isn't fixed. However,
    >> in that case, one wonders why the presence of /size/
    >> doesn't do the trick? Could someone please tell me what [*] is for then?

    >
    > With C99 VLAs, you can do
    >
    > void foo (int size, char arr[size])
    > {
    > ....
    > }
    >
    > In a prototype of foo,
    > void foo (int, char [*]);
    > void foo (int size, char arr[*]);
    > void foo (int size, char arr[size]);
    > are essentially equivalent. The asterisk can only be used in
    > function prototypes.
    > char [*] is a VLA of (yet) unknown number of elements.
    > char [] is just a pointer to char in a function prototype.



    > void foo (int size, char arr[size])


    Excellent - thus the meaning of Steele/Harbinson's comment 'When an array's
    length is also a parameter, it must necessarily appear first due to C's
    lexical scoping rules'


    --
    ==============
    Not a pedant
    ==============
     
    pemo, Feb 20, 2006
    #5
  6. Robin Haigh Guest

    <> wrote in message
    news:...
    > In f(), I am passing a parameter of array type - /size/ indicates the
    > array's length:
    >
    > void f(char arr[], int size);


    There isn't really such a thing as an array parameter. The notation char
    arr[], which is just an alternative way of writing char *arr in this
    situation, was described by Ritchie as a living fossil -- just a hangover
    from the predecessors of C.

    There isn't any point in telling the compiler the size of arr, whatever that
    might mean -- it can't use the information.

    But if you pass a 2-dimensional array char a[10][80] to a function defined
    as

    void f(char arr[][80]) {}

    or equivalently

    void f(char (*arr)[80]) {}

    the compiler needs the 2nd dimension, when compiling f, to do pointer
    arithmetic, though it still hasn't any use for the 1st dimension. In C99
    you can make the 2nd dimension variable:

    void f(int cols, char (*arr)[cols]) {}

    and in a prototype, if you don't want to use dummy names, you can do

    void f(int, char(*)[*]);

    or if you prefer

    void f(int, char[][*]);


    --
    RSH
     
    Robin Haigh, Feb 20, 2006
    #6
  7. Jack Klein Guest

    On Mon, 20 Feb 2006 17:44:55 -0000, "pemo" <>
    wrote in comp.lang.c:

    > pemo wrote:


    [big snip]

    > Using gcc, this compiles fine...
    >
    > void f(char arr[]);


    And always has, at least since prototypes were added to the language
    in 1989. This is of course exactly equivalent to:

    void f(char *arr);

    > void f(char arr[])
    > {
    > ...
    > }


    ....as is the body.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Feb 21, 2006
    #7
    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. E. Robert Tisdale

    variable length arrays

    E. Robert Tisdale, Sep 27, 2003, in forum: C Programming
    Replies:
    21
    Views:
    774
    Mark McIntyre
    Oct 13, 2003
  2. jaime

    Initialization of variable length arrays

    jaime, Jun 15, 2007, in forum: C Programming
    Replies:
    3
    Views:
    1,179
    Ian Collins
    Jun 15, 2007
  3. Erwin Lindemann

    Variable length arrays

    Erwin Lindemann, Mar 6, 2008, in forum: C Programming
    Replies:
    45
    Views:
    1,170
    Keith Thompson
    Mar 8, 2008
  4. Philipp
    Replies:
    21
    Views:
    1,190
    Philipp
    Jan 20, 2009
  5. Rui Maciel
    Replies:
    56
    Views:
    2,750
    Tim Rentsch
    Jul 10, 2012
Loading...

Share This Page