Macros With A Variable Number Of Arguments - Red Hat ENTERPRISE LINUX 3 - USING GCC Using Instructions

Using the gnu compiler collection (gcc)
Hide thumbs Also See for ENTERPRISE LINUX 3 - USING GCC:
Table of Contents

Advertisement

Chapter 6. Extensions to the C Language Family
6.14. Arrays of Variable Length
Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them
in C89 mode and in C++. (However, GCC's implementation of variable-length arrays does not yet
conform in detail to the ISO C99 standard.) These arrays are declared like any other automatic arrays,
but with a length that is not a constant expression. The storage is allocated at the point of declaration
and deallocated when the brace-level is exited. For example:
FILE *
concat_fopen (char *s1, char *s2, char *mode)
{
char str[strlen (s1) + strlen (s2) + 1];
strcpy (str, s1);
strcat (str, s2);
return fopen (str, mode);
}
Jumping or breaking out of the scope of the array name deallocates the storage. Jumping into the
scope is not allowed; you get an error message for it.
You can use the function
is available in many other C implementations (but not in all). On the other hand, variable-
alloca
length arrays are more elegant.
There are other differences between these two methods. Space allocated with
the containing function returns. The space for a variable-length array is deallocated as soon as the
array name's scope ends. (If you use both variable-length arrays and
deallocation of a variable-length array will also deallocate anything more recently allocated with
.)
alloca
You can also use variable-length arrays as arguments to functions:
struct entry
tester (int len, char data[len][len])
{
/* ... */
}
The length of an array is computed once when the storage is allocated and is remembered for the
scope of the array in case you access it with
If you want to pass the array first and the length afterward, you can use a forward declaration in the
parameter list--another GNU extension.
struct entry
tester (int len; char data[len][len], int len)
{
/* ... */
}
The
before the semicolon is a parameter forward declaration, and it serves the purpose of
int len
making the name
len
You can write any number of such parameter forward declarations in the parameter list. They can be
separated by commas or semicolons, but the last one must end with a semicolon, which is followed
by the "real" parameter declarations. Each forward declaration must match a "real" declaration in
parameter name and data type. ISO C99 does not support parameter forward declarations.
to get an effect much like variable-length arrays. The function
alloca
known when the declaration of
.
sizeof
is parsed.
data
exists until
alloca
in the same function,
alloca
131

Advertisement

Table of Contents
loading

This manual is also suitable for:

Enterprise linux 3

Table of Contents