By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,514 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,514 IT Pros & Developers. It's quick & easy.

allocate an array

P: n/a
Hello everybody,

I've got a problem. I'm trying to allocate an array inside a function with
Parameter int n:
main(){
int i;
....
i = strlen(ch);
array(i);
....
}
int array ( int n ){
int a[n];
....
}
but when i compile:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'a' : unknown size
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
how can i do now?
Thank u in advance for your afforts.
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Simon wrote:
Hello everybody,
int array ( int n ){
int a[n];
...
}
but when i compile:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'a' : unknown size
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0


Perhaps because your compiler does not accept C99 ?
To my knowledge, this construction is in C99, but not
in C89.
Change your compiler, or use "malloc".

Marc Boyer
--
Lying for having sex or lying for making war? Trust US presidents :-(
Nov 13 '05 #2

P: n/a
Simon wrote:
I've got a problem. I'm trying to allocate an array inside a function with
Parameter int n:
main(){
int i;
...
i = strlen(ch);
array(i);
...
}
int array ( int n ){
int a[n];
...
}


You can't do that in (pre-99) C. Instead of allocating an array,
allocate some heap space (and remember to free it), eg:

int *a = malloc( n * sizeof (*a) );

Remember to #include <stdlib.h>.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #3

P: n/a
thanks a lot.

i got it

"Chris Dollin" <ke**@hpl.hp.com> schrieb im Newsbeitrag
news:be**********@murdoch.hpl.hp.com...
Simon wrote:
I've got a problem. I'm trying to allocate an array inside a function with Parameter int n:
main(){
int i;
...
i = strlen(ch);
array(i);
...
}
int array ( int n ){
int a[n];
...
}


You can't do that in (pre-99) C. Instead of allocating an array,
allocate some heap space (and remember to free it), eg:

int *a = malloc( n * sizeof (*a) );

Remember to #include <stdlib.h>.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html

Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.