Velocity Reviews > Variable length arrays Q

# Variable length arrays Q

Guest
Posts: n/a

 02-20-2006
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

pemo
Guest
Posts: n/a

 02-20-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
Guest
Posts: n/a

 02-20-2006
pemo wrote:
> (E-Mail Removed) 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
==============

Michael Mair
Guest
Posts: n/a

 02-20-2006
(E-Mail Removed) 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.

pemo
Guest
Posts: n/a

 02-20-2006
Michael Mair wrote:
> (E-Mail Removed) 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
==============

Robin Haigh
Guest
Posts: n/a

 02-20-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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

Jack Klein
Guest
Posts: n/a

 02-21-2006
On Mon, 20 Feb 2006 17:44:55 -0000, "pemo" <(E-Mail Removed)>
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/~a...FAQ-acllc.html