473,406 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Variable length arrays Q

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 '06 #1
6 1829
me*********@googlemail.com 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
==============
Feb 20 '06 #2
pemo wrote:
me*********@googlemail.com 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
==============
Feb 20 '06 #3
me*********@googlemail.com 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.
Feb 20 '06 #4
Michael Mair wrote:
me*********@googlemail.com 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
==============
Feb 20 '06 #5

<me*********@googlemail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr 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

Feb 20 '06 #6
On Mon, 20 Feb 2006 17:44:55 -0000, "pemo" <us***********@gmail.com>
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
Feb 21 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: steflhermitte | last post by:
Dear cpp-ians, I am working with a structure: struct meta_segment { long double id; long double num; long double mean; bool done;
10
by: yawnmoth | last post by:
i've written some javascript code that i believe should set a form variable to a certain value, depending on what the user clicks on. unfortunately, it isn't working. here's the url: ...
27
by: Mike P | last post by:
I will be passing my function a two dimensional array of varying length. Within that array is one data point, and the number of times it should loop through. So, for example, I might pass this...
5
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h>...
6
by: Carl-Olof Almbladh | last post by:
Already in the 1st edition of the "White book", Kerigham and Ritchie states the "C is a general purpose language". However, without what is usually called "assumed size arrays" and built-in...
6
by: JNY | last post by:
Hello, Is it possible to declare an array with variable indeces? i.e. int x = 4; int myArray; for (j = 0;j < 5;j++) {
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
3
by: jaime | last post by:
Hi all. The source code download bundle for "Beginning C: From Novice to Professional, Fourth Edition" (ISBN: 1590597354) (Horton/Apress) contains a C source file (program9_09.c) which contains...
7
by: Cromulent | last post by:
In section 6.7.5.2 it states the following: If the size is not present, the array type is an incomplete type. If the size is*instead of being an expression, the array type is a variable length...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.