int array

Discussion in 'C Programming' started by Matthew Jakeman, Aug 30, 2004.

  1. Hi, if i define an int array globally as follows :

    int test[] ;

    Is it possible to set the size of this array inside a function later in the
    code ?

    TIA
    Matt
     
    Matthew Jakeman, Aug 30, 2004
    #1
    1. Advertising

  2. "Matthew Jakeman" <> wrote in
    news:cgvb17$kcb$:

    > Hi, if i define an int array globally as follows :
    >
    > int test[] ;


    >
    > Is it possible to set the size of this array inside a function later in
    > the code ?


    No.

    --
    - Mark ->
    --
     
    Mark A. Odell, Aug 30, 2004
    #2
    1. Advertising

  3. In article <Xns9555663793102CopyrightMarkOdell@130.133.1.4>, Mark A.
    Odell wrote:
    > "Matthew Jakeman" <> wrote in
    > news:cgvb17$kcb$:
    >
    >> Hi, if i define an int array globally as follows :
    >>
    >> int test[] ;
    >>
    >> Is it possible to set the size of this array inside a function later
    >> in
    >> the code ?

    >
    > No.


    If the size of the array is only known at run-time, you need to allocate
    memory for the array at run-time. This is called "dynamic allocation"
    (if you specify the array size at compile-time (e.g. int test[3]) it is
    called "static allocation").

    I advice you to consult a C book on these topics.
    Regards,
    --
    Rob van der Leek | rob(at)ricardis(dot)tudelft(dot)nl
    Ricardishof 73-A | http://www.ricardis.tudelft.nl/~rob
    2614 JE Delft, The Netherlands
    +31 (0)6 155 244 60
     
    Rob van der Leek, Aug 30, 2004
    #3
  4. Rob van der Leek <> spoke thus:

    > If the size of the array is only known at run-time, you need to allocate
    > memory for the array at run-time. This is called "dynamic allocation"
    > (if you specify the array size at compile-time (e.g. int test[3]) it is
    > called "static allocation").


    I believe the size of OP's array is known at compile time; the issue
    is that there is no way to determine the size of an array outside the
    scope in which it is declared, which seems to be what OP wants to do.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Aug 30, 2004
    #4
  5. Matthew Jakeman

    Dan Pop Guest

    In <cgvb17$kcb$> "Matthew Jakeman" <> writes:

    >Hi, if i define an int array globally as follows :
    > ^^^^^^
    >int test[] ;
    >
    >Is it possible to set the size of this array inside a function later in the
    >code ?


    An object *definition* is a declaration that also allocates memory for
    that object. How can the compiler allocate memory for test[] without
    knowing its size?

    What you can do is *declaring* test[] as a global without specifying its
    size:

    extern int test[];

    and define it elsewhere in your code. Note that you cannot use
    sizeof test until the compiler has seen the actual definition or a
    declaration also specifying the size.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Aug 30, 2004
    #5
  6. Matthew Jakeman

    CBFalconer Guest

    Rob van der Leek wrote:
    > Mark A. Odell wrote:
    >> "Matthew Jakeman" <> wrote in
    >>
    >>> Hi, if i define an int array globally as follows :
    >>>
    >>> int test[] ;
    >>>
    >>> Is it possible to set the size of this array inside a function
    >>> later in the code ?

    >>
    >> No.

    >
    > If the size of the array is only known at run-time, you need to
    > allocate memory for the array at run-time. This is called
    > "dynamic allocation" (if you specify the array size at compile-
    > time (e.g. int test[3]) it is called "static allocation").
    >
    > I advice you to consult a C book on these topics.


    Note that Mark replied to a query that dealt with 'globally'
    defined arrays, which can be loosely interpreted to mean
    statically allocated.

    --
    "Churchill and Bush can both be considered wartime leaders, just
    as Secretariat and Mr Ed were both horses." - James Rhodes.
    "We have always known that heedless self-interest was bad
    morals. We now know that it is bad economics" - FDR
     
    CBFalconer, Aug 30, 2004
    #6
    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. S300
    Replies:
    4
    Views:
    22,049
    Roedy Green
    Aug 19, 2003
  2. Schnoffos
    Replies:
    2
    Views:
    1,237
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,686
    Old Wolf
    Jan 20, 2004
  4. arun
    Replies:
    8
    Views:
    468
    Dave Thompson
    Jul 31, 2006
  5. Alain Spineux
    Replies:
    6
    Views:
    644
    Eric Sosman
    May 17, 2011
Loading...

Share This Page