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

declaring arrays with variable indeces

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
Nov 14 '05 #1
6 2041
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.

--
-trent
Programmer - n. One who makes the lies the salesman told come true.
Nov 14 '05 #2
Trent Buck <ge*******@tznvy.pbz> wrote:
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
Nov 14 '05 #3
/* 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);

JNY wrote:
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


Nov 14 '05 #4
JNY wrote:
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.
Nov 14 '05 #5
Maja wrote:
/* 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.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #6
jn**@hotmail.com (JNY) writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7

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

Similar topics

29
by: Friday | last post by:
Sorry if this is the wrong group. I tried to find the one I thought would be most relevant. I'm an old PHP guy, who knows little about asp and NOTHING about asp.net, but need to learn at least...
4
by: Lucy | last post by:
In the Declarations section of a form's code module, what is the difference between the following: Dim Flag As Boolean Public Flag As Boolean Private Flag As Boolean Thanks!
14
by: Eric Bantock | last post by:
Very basic question I'm afraid. Once an array has been declared, is there a less tedious way of assigning values to its members than the following: myarray=8; myarray=3; myarray=4; myarray=0;...
3
by: mark | last post by:
When I declare an array as double(,) then try to use it I get an error: "Object reference not set to an instance of an object." I have found that I can redim the array and all is well. Is my...
1
by: John Dann | last post by:
Is there a way of declaring an array of a structure where the structure has a constructor? So if I had a structure say Friend MyStructure prop1 as integer prop2 as string sub New (p1 as...
3
by: mamun | last post by:
Hi all, I am trying to create variables dynamically. This is needed because the user interface can have ten different textboxes with name as txt1, txt2 and so on. I would like to get values of...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
5
by: Rahul B | last post by:
Hi, I have very little knowledge about creating Procedures/functions in DB2. When i tried to create the test function like CREATE FUNCTION GET_TEST (P_TEST_ID INTEGER, P_SEL_OR_SORT...
5
by: miladhatam | last post by:
how can i declare a varible dynamically like this : for (i=1 ; i<10;i++){ int ("j" + i) = i ; //declaring j1 - j9 } ofcourse this code is wrong and it is an algorithm
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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:
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...
0
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,...

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.