Variable length arrays Q

M

mechanicfem

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
 
P

pemo

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[])
{
...
}
 
P

pemo

pemo said:
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?
 
M

Michael Mair

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
 
P

pemo

Michael said:
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'
 
R

Robin Haigh

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[][*]);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top