473,385 Members | 1,409 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,385 software developers and data experts.

Variable Length Arrays

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 array type of
unspeciļ¬ed size,
which can only be used in declarations with function prototype
scope;124) such arrays are
nonetheless complete types. If the size is an integer constant
expression and the element
type has a known constant size, the array type is not a variable length
array type;
otherwise, the array type is a variable length array type.

5 If the size is an expression that is not an integer constant
expression: if it occurs in a
declaration at function prototype scope, it is treated as if it were
replaced by*;otherwise,
each time it is evaluated it shall have a value greater than zero. The
size of each instance
of a variable length array type does not change during its lifetime.
Where a size
expression is part of the operand of a sizeof operator and changing the
value of the
size expression would not affect the result of the operator,
itisunspeciļ¬ed whether or not
the size expression is evaluated.

but I am having problems understanding the purpose of an incomplete
type. What is the purpose and why would someone use one rather than
specifying an int for the size of the VLA?

How does it affect the operation of the array? Does it change how it is
treated by the program?

Thanks for any help.

--
"I disapprove of what you say, but I'll defend to the death your right
to say it." - Voltaire

Aug 5 '08 #1
7 2774
On 2008-08-05 10:04:30 +0100, Cromulent <cr*******@justextrememetal.comsaid:
<snip>
I am assuming from the lack of responses that I am being increadibly
stupid with that question.

Okay let me rephrase the question is this the correct interpretation of
the standard text quoted in my original post?

int blah_func(int somesize, int arr[*]); /* This is the only legal use
of * in an array declaration */

int blah_func(int somesize, int arr[somesize])
{

/* arr[] is an array of variable size specified by the integer somesize */

return 0;
}

I'm just trying to find out what I am missing here really.

--
"I disapprove of what you say, but I'll defend to the death your right
to say it." - Voltaire

Aug 5 '08 #2
In article <2008080510043016807-cromulent@justextrememetalcom>,
Cromulent <cr*******@justextrememetal.comwrote:
>but I am having problems understanding the purpose of an incomplete
type. What is the purpose and why would someone use one rather than
specifying an int for the size of the VLA?
VLAs are new in C99; incomplete types are the protocol for C95
and earlier.

Besides, you don't always know the array size accurately.
The purpose of the routine might be to -determine- the array
size by searching for a particular marker (e.g., strlen)
--
"I buy more from my grocer than he buys from me, and I bet it's
the same with you and your grocer. That means we have a trade
deficit with our grocers. Does our perpetual grocer trade deficit
portend doom?" -- Walter Williams
Aug 5 '08 #3
Cromulent <cr*******@justextrememetal.comwrites:
On 2008-08-05 10:04:30 +0100, Cromulent <cr*******@justextrememetal.comsaid:
><snip>

I am assuming from the lack of responses that I am being increadibly
stupid with that question.

Okay let me rephrase the question is this the correct interpretation
of the standard text quoted in my original post?

int blah_func(int somesize, int arr[*]); /* This is the only legal use
of * in an array declaration */
All it is there for is so that you don't have to repeat the parameter
name that gives the size. It also gives you a notation to use when
you don't want any names, but that only pays off with 2D arrays.

In your case:

int blah_func(int somesize, int arr[somesize]);
int blah_func(int, int arr[*]);
int blah_func(int, int arr[]);
int blah_func(int, int *arr);

are all the same, but with a 2D array you need to say that the size of
the row is determined by a parameter. I.e.:

f(int, int, int (*)[*]);
f(int n, int m, int (*)[m]);
f(int n, int m, int mat[n][m]);
f(int n, int m, int mat[*][*]);
f(int, int, int mat[*][*]);

are all the same, but not at all the same as:

f(int, int, int **);
f(int n, int m, int mat[][]);

The first is just another function altogether and the second is not
valid as a declaration. The * lets to show the "arrayness" of a
multidimensional array parameter without having to repeat (or
specify) the names of the sizes. I imagine the most common cases
you'll see are:

f(int n, int m, int mat[*][*]);
f(int, int, int mat[*][*]);

and sometimes:

f(int, int, int (*)[*]);
I'm just trying to find out what I am missing here really.
You need to ask a more specific question.

--
Ben.
Aug 5 '08 #4
Cromulent wrote, On 05/08/08 17:25:
On 2008-08-05 10:04:30 +0100, Cromulent <cr*******@justextrememetal.com>
said:
><snip>

I am assuming from the lack of responses that I am being increadibly
stupid with that question.
No, the reason for lack of response is that you have not allowed enough
time. For example I was driving to a customer site when you first
posted, worked there without internet access, drove back to my office
where I had to do my expenses and whilst I was doing my expenses you
made this second post.
Okay let me rephrase the question is this the correct interpretation of
the standard text quoted in my original post?

int blah_func(int somesize, int arr[*]); /* This is the only legal use
of * in an array declaration */
Yes, that is my reading of what you quoted from the standard as well.
int blah_func(int somesize, int arr[somesize])
{

/* arr[] is an array of variable size specified by the integer
somesize */
No, it is the same as if you had used "int add[*]" because of this part
of what you posted...
| 5 If the size is an expression that is not an integer constant
| expression: if it occurs in a declaration at function prototype scope,
| it is treated as if it were replaced by*;
return 0;
}

I'm just trying to find out what I am missing here really.
See above for at least one thing you were missing.

You also asked about incomplete types in your original post. There are a
completely different kettle of fish from your examples above.

You could do something like the following:

/* foo.h */
extern int arr[]; /* incomplete */

/* foo.c */
#include "foo.h"
int arr[5];

Now other files can include foo.h and they will not know how big the
array is.
--
Flash Gordon
Aug 5 '08 #5
On 2008-08-05 19:40:10 +0100, Flash Gordon <sp**@flash-gordon.me.uksaid:
Cromulent wrote, On 05/08/08 17:25:
>On 2008-08-05 10:04:30 +0100, Cromulent <cr*******@justextrememetal.comsaid:
>><snip>

I am assuming from the lack of responses that I am being increadibly
stupid with that question.

No, the reason for lack of response is that you have not allowed enough
time. For example I was driving to a customer site when you first
posted, worked there without internet access, drove back to my office
where I had to do my expenses and whilst I was doing my expenses you
made this second post.
Ah, sorry. Patience is not a virtue I seem to have been blessed with :).
>
>Okay let me rephrase the question is this the correct interpretation of
the standard text quoted in my original post?

int blah_func(int somesize, int arr[*]); /* This is the only legal use
of * in an array declaration */

Yes, that is my reading of what you quoted from the standard as well.
>int blah_func(int somesize, int arr[somesize])
{

/* arr[] is an array of variable size specified by the integer somesize */

No, it is the same as if you had used "int add[*]" because of this part
of what you posted...
| 5 If the size is an expression that is not an integer constant
| expression: if it occurs in a declaration at function prototype scope,
| it is treated as if it were replaced by*;
> return 0;
}

I'm just trying to find out what I am missing here really.

See above for at least one thing you were missing.
Hmm okay. Thanks for pointing that out. I think I see how these work now.
>
You also asked about incomplete types in your original post. There are
a completely different kettle of fish from your examples above.

You could do something like the following:

/* foo.h */
extern int arr[]; /* incomplete */

/* foo.c */
#include "foo.h"
int arr[5];

Now other files can include foo.h and they will not know how big the array is.
This is what I get lost with. What is the point of declaring an array,
and then initilising it and then being able to use the same array in
another file which has no idea of the size of the array?

Thank you both for the help.
--
"I disapprove of what you say, but I'll defend to the death your right
to say it." - Voltaire

Aug 5 '08 #6
In article <2008080522164316807-cromulent@justextrememetalcom>
Cromulent <cr*******@justextrememetal.comwrote:
>... What is the point of declaring an array, and then initilising
it and then being able to use the same array in another file which
has no idea of the size of the array?
Here is an example of how one might use that:

% cat file1.c
#include "header.h"

void do_something(void) {
size_t i;

for (i = 0; i < size_of_array; i++)
operate(array[i]);
}
/* presumably more stuff here */
% cat file2.c
#include "header.h"

double array[] = {
3.14159265358979323846264338327950,
2.71828182845904523536028747135266,
42.0
};

size_t size_of_array = sizeof array / sizeof *array;
/* possibly more stuff here */
% cat header.h
#include <stddef.h>
extern double array[];
extern size_t size_of_array;

void do_something(void);
void operate(double);
/* more stuff here as needed */
%

You can now change the behavior of the program by changing only
file2.c (and recompiling appropriate files), without having to
change file1.c (which, if this were a more realistic example, might
be quite a bit larger than shown above).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
Aug 6 '08 #7
On 2008-08-06 10:07:39 +0100, Chris Torek <no****@torek.netsaid:
In article <2008080522164316807-cromulent@justextrememetalcom>
Cromulent <cr*******@justextrememetal.comwrote:
>... What is the point of declaring an array, and then initilising
it and then being able to use the same array in another file which
has no idea of the size of the array?

<snip>

You can now change the behavior of the program by changing only
file2.c (and recompiling appropriate files), without having to
change file1.c (which, if this were a more realistic example, might
be quite a bit larger than shown above).
Ah ha! Thanks, so it is a way of producing modular code then that can
act on an array without having the specifics of said array then?
--
"I disapprove of what you say, but I'll defend to the death your right
to say it." - Voltaire

Aug 6 '08 #8

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) { ..... }
6
by: mechanicfem | last post by:
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...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.