declaring arrays with variable indeces

J

JNY

Hello,

Is it possible to declare an array with variable indeces?
i.e.

int x = 4;
int myArray[x];

for (j = 0;j < 5;j++)
{
myArray[j] = j;
}

x = 5;

for (j = 0;j < 6;j++)
{
myArray[j] = j;
}

If it is possible I may have to use pointers, but I'm not sure how,
and searching this group didn't come up with any answers. If anyone
knows, I'd grately appreciate their ideas.

Cheers,
JNY
 
T

Trent Buck

Up spake JNY:
Is it possible to declare an array with variable index?

int x = 4;
int myArray[x];

From the GCC manual:

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.

Run "info gcc 'C Extensions' 'Variable Length'" to read the full thing.
 
R

Richard Bos

Trent Buck said:
Up spake JNY:
Is it possible to declare an array with variable index?

int x = 4;
int myArray[x];

From the GCC manual:

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.)
Run "info gcc 'C Extensions' 'Variable Length'" to read the full thing.

Assuming you have both gcc and info. Alternatively, try the latest
public draft of the Standard, which (last-minute changes excepted)
applies to _all_ C99 compilers, not just to Ganuck, and can be found at
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n869/>.

Richard
 
M

Maja

/* Suggestion: declare an integer pointer and grab the memory
you need. */

int *myArray;
int j;
int x = 4;

myArray = (int *) malloc (sizeof(int) * x);

/* Note: regardless of how you declare your integer array,
indexing the array beyond it's size (as you did below) is a no no.
Remember: myArray[4] is the 5th element. */

/* When using malloc, don't forget to clean up your room. */
free (myArray);
 
E

E. Robert Tisdale

JNY said:
Is it possible to declare an array with variable indices?
No.

i.e.

int x = 4;
int myArray[x];

for (j = 0; j < x; ++j) {
myArray[j] = j;
}

x = 5;

This does *not* change the size of myArray.
Once memory has been allocated for a variable size array,
the size cannot be changed.
for (j = 0; j < x; ++j) { // error
myArray[j] = j;
}

If it is possible I may have to use pointers,
but I'm not sure how,
and searching this group didn't come up with any answers.
If anyone knows, I'd grately appreciate their ideas.

You are confused.
You probably don't need to change the size of your array.
But you can ask about realloc(void*, size_t)
if you are interested.
 
A

Andrey Tarasevich

Maja said:
/* Suggestion: declare an integer pointer and grab the memory
you need. */

int *myArray;
int j;
int x = 4;

myArray = (int *) malloc (sizeof(int) * x);

/* Suggestion: do not cast the return value of 'malloc' */
/* Suggestion: use 'expression' form of 'sizeof' whenever possible */
myArray = malloc(x * sizeof *myArray);
/* Note: regardless of how you declare your integer array,
indexing the array beyond it's size (as you did below) is a no no.
Remember: myArray[4] is the 5th element. */

/* When using malloc, don't forget to clean up your room. */
free (myArray);

JNY wrote:
...

Suggestion: do not top post in Usenet newsgroups.
 
K

Keith Thompson

Is it possible to declare an array with variable indeces?
i.e.

int x = 4;
int myArray[x];

for (j = 0;j < 5;j++)
{
myArray[j] = j;
}

x = 5;

for (j = 0;j < 6;j++)
{
myArray[j] = j;
}

In C99 (but not in C90), you can declare a variable-length array
(VLA). The length of a VLA can be a non-constant expression, but it's
evaluated when the array is created. The size of the array cannot be
changed after that. In your sample code, changing the value of x
doesn't affect the array.

Note that the C99 standard is not yet widely implemented. Pre-C99
compilers don't support VLAs (though gcc has its own, slightly
incompatible, version of variable-length arrays).

If you know your array can never exceed some constant size, you can
just declare an array of that size and keep track of how much of
you're currenty using. Otherwise, you can use malloc() for the
initial allocation and realloc() to adjust the size.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top