473,778 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
unspecified 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,
itisunspecifie d 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 2798
On 2008-08-05 10:04:30 +0100, Cromulent <cr*******@just extrememetal.co msaid:
<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 <20080805100430 16807-cromulent@juste xtrememetalcom> ,
Cromulent <cr*******@just extrememetal.co mwrote:
>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*******@just extrememetal.co mwrites:
On 2008-08-05 10:04:30 +0100, Cromulent <cr*******@just extrememetal.co msaid:
><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
multidimensiona l 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*******@just extrememetal.co m>
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.uksai d:
Cromulent wrote, On 05/08/08 17:25:
>On 2008-08-05 10:04:30 +0100, Cromulent <cr*******@just extrememetal.co msaid:
>><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 <20080805221643 16807-cromulent@juste xtrememetalcom>
Cromulent <cr*******@just extrememetal.co mwrote:
>... 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(vo id) {
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.1415926535897 932384626433832 7950,
2.7182818284590 452353602874713 5266,
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(vo id);
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 (4039.22'N, 11150.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.n etsaid:
In article <20080805221643 16807-cromulent@juste xtrememetalcom>
Cromulent <cr*******@just extrememetal.co mwrote:
>... 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
1754
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
1768
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: http://www.frostjedi.com/terra/scripts/graemlin3.html clicking in the color palette thing and then clicking the submit button reveals that the form variable named color isn't being set. if you look at the javascript source, you'll see that i'm atleast...
27
2634
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 to the function: example = new Array("A",2); example = new Array("Q",4); function loopIt(example);
5
14114
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> #include<stdlib.h> int main(void) { int y = { 7, 9,10},i; for (;i<20;i++)
6
3172
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 complex, C is not entirely suitable for numerical work. In particular, in order to have functions which can manipulate matrices whose dimensions are not determined at compile time one needs pointers of the form, say, double (*a) where n is not a...
6
2070
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
19203
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
1855
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 used to indicate that an array parameter has /variable length/ - so, f() may be rewitten as: void f(char arr, int size);
3
4911
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 several instances of the following type of idiom: /* Program 9.9 REVERSI An Othello type game */ const int SIZE = 6;
0
9629
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9470
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10127
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10069
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7475
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4033
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.