allocate an array

Discussion in 'C Programming' started by Simon, Jul 3, 2003.

  1. Simon

    Simon Guest

    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.
    Simon, Jul 3, 2003
    #1
    1. Advertising

  2. Simon

    Marc Boyer Guest

    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 :-(
    Marc Boyer, Jul 3, 2003
    #2
    1. Advertising

  3. Simon

    Chris Dollin Guest

    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-newsgroup/comp/comp.lang.c.html
    C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
    Chris Dollin, Jul 3, 2003
    #3
  4. Simon

    Simon Guest

    thanks a lot.

    i got it

    "Chris Dollin" <> schrieb im Newsbeitrag
    news:be16n0$6lj$...
    > 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-newsgroup/comp/comp.lang.c.html
    > C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
    Simon, Jul 3, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas G. Marshall

    Re: more than 10 seconds to allocate an array!

    Thomas G. Marshall, Aug 28, 2003, in forum: Java
    Replies:
    2
    Views:
    377
    Ahmed Moustafa
    Aug 28, 2003
  2. Andy Flowers
    Replies:
    34
    Views:
    970
    Thomas G. Marshall
    Sep 6, 2003
  3. Jon Skeet
    Replies:
    6
    Views:
    376
    Ahmed Moustafa
    Aug 30, 2003
  4. Mark Thornton
    Replies:
    1
    Views:
    341
    Ahmed Moustafa
    Sep 1, 2003
  5. valerio
    Replies:
    3
    Views:
    359
Loading...

Share This Page