static array

Discussion in 'C Programming' started by dis_is_eagle@yahoo.com, Jul 28, 2006.

  1. Guest

    hi...i am new to c programming....please explain me as to why should
    an character array be declared as static...thanx...eric
     
    , Jul 28, 2006
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > hi...i am new to c programming....please explain me as to why should
    > an character array be declared as static...thanx...eric
    >

    In what context?

    The same rules apply for a character array as for any other variable.

    If the array is in a compilation unit and you don't want it to be
    visible elsewhere, you declare it static to avoid polluting the global
    namespace with the array's name.

    If the array has function scope and you wish the value to be retained
    between calls, you would declare it static.

    --
    Ian Collins.
     
    Ian Collins, Jul 28, 2006
    #2
    1. Advertising

  3. wrote:
    > hi...i am new to c programming....please explain me as to why should
    > an character array be declared as static...thanx...eric


    In the earlier days, perhaps before C99, a local array could not be
    initialized unless it was declared as a static one.
     
    lovecreatesbeauty, Jul 28, 2006
    #3
  4. Guest

    wrote:

    > hi...i am new to c programming....please explain me as to why should
    > an character array be declared as static...thanx...eric


    One possibility is that you want the value to be remembered
    during subsequent calls to the function. This applies to all kinds
    of static variables not just character arrays. For another possibility
    read 7.5a and 7.5b of the FAQ.

    http://c-faq.com/malloc/retaggr.html

    Spiros Bousbouras
     
    , Jul 28, 2006
    #4
  5. lovecreatesbeauty said:

    >
    > wrote:
    >> hi...i am new to c programming....please explain me as to why should
    >> an character array be declared as static...thanx...eric

    >
    > In the earlier days, perhaps before C99, a local array could not be
    > initialized unless it was declared as a static one.


    Have you any evidence to support this refreshingly creative explanation?

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jul 28, 2006
    #5
  6. Flash Gordon Guest

    lovecreatesbeauty wrote:
    > wrote:
    >> hi...i am new to c programming....please explain me as to why should
    >> an character array be declared as static...thanx...eric

    >
    > In the earlier days, perhaps before C99, a local array could not be
    > initialized unless it was declared as a static one.


    In C89 you could initialise a local array. I don't know about pre-ANSI
    C, but that really was a long time ago.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, Jul 28, 2006
    #6
  7. osmium Guest

    "Ian Collins" writes:

    >> hi...i am new to c programming....please explain me as to why should
    >> an character array be declared as static...thanx...eric
    >>

    > In what context?
    >
    > The same rules apply for a character array as for any other variable.
    >
    > If the array is in a compilation unit and you don't want it to be
    > visible elsewhere, you declare it static to avoid polluting the global
    > namespace with the array's name.
    >
    > If the array has function scope and you wish the value to be retained
    > between calls, you would declare it static.


    A static array will auomatically have all elements reset to 0.
     
    osmium, Jul 28, 2006
    #7
  8. osmium said:

    > "Ian Collins" writes:
    >
    >>> hi...i am new to c programming....please explain me as to why should
    >>> an character array be declared as static...thanx...eric
    >>>

    >> In what context?
    >>
    >> The same rules apply for a character array as for any other variable.
    >>
    >> If the array is in a compilation unit and you don't want it to be
    >> visible elsewhere, you declare it static to avoid polluting the global
    >> namespace with the array's name.
    >>
    >> If the array has function scope and you wish the value to be retained
    >> between calls, you would declare it static.

    >
    > A static array will auomatically have all elements reset to 0.


    Not if it has function scope. If it's not explicitly initialised, then yes,
    they will be /set/ to 0 at program start, but they will never be
    automatically /reset/ to 0.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jul 28, 2006
    #8
  9. Flash Gordon wrote:
    > lovecreatesbeauty wrote:
    > > In the earlier days, perhaps before C99, a local array could not be
    > > initialized unless it was declared as a static one.

    > In C89 you could initialise a local array. I don't know about pre-ANSI
    > C, but that really was a long time ago.


    Yes, perhaps C89 already does it. In H&S5 sec4.6.4, it says that
    (translated from Chinese edition) static arrays and external arrays can
    be initialized in this way... Automatic arrays are allowed to be
    initialized in Standard C [1], but the original C definition [2] did
    not support this feature.

    In Stephen Prata's C Primer Plus sec10.1.1, the author mentions the
    similar thing also.

    P.S. Hi Richard, why are you always in high dudgeon on c.l.c? Are you
    the same in your real life? Could you clarify following doubts for me,
    for I'm not an expert and do not have an English H&S5?

    [1] I do not know what does "Standard C" mean in the Chinese edition of
    the book, but I guess it means C89 now.
    [2] pre-Ansi, K&R C or Classical C. The Chinese book does not state it
    clear enough.
     
    lovecreatesbeauty, Jul 28, 2006
    #9
  10. lovecreatesbeauty said:

    >
    > Flash Gordon wrote:
    >> lovecreatesbeauty wrote:
    >> > In the earlier days, perhaps before C99, a local array could not be
    >> > initialized unless it was declared as a static one.

    >> In C89 you could initialise a local array. I don't know about pre-ANSI
    >> C, but that really was a long time ago.

    >
    > Yes, perhaps C89 already does it.


    There's no "perhaps" about it.

    > P.S. Hi Richard, why are you always in high dudgeon on c.l.c?


    I don't suffer fools gladly.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jul 28, 2006
    #10
  11. Richard Heathfield wrote:
    > lovecreatesbeauty said:
    > > P.S. Hi Richard, why are you always in high dudgeon on c.l.c?

    >
    > I don't suffer fools gladly.


    Have you shown people whether you are a fool or not? Why do you have to
    suffer from anything?
     
    lovecreatesbeauty, Jul 28, 2006
    #11
  12. On Fri, 28 Jul 2006 10:00:44 +0000, Richard Heathfield
    <> wrote:

    > lovecreatesbeauty said:
    >
    > >
    > > wrote:
    > >> hi...i am new to c programming....please explain me as to why should
    > >> an character array be declared as static...thanx...eric

    > >
    > > In the earlier days, perhaps before C99, a local array could not be
    > > initialized unless it was declared as a static one.

    >
    > Have you any evidence to support this refreshingly creative explanation?


    FAQ 1.31. Prestandard is indeed before C99 -- by quite a lot. <G?>
    - David.Thompson1 at worldnet.att.net
     
    Dave Thompson, Aug 14, 2006
    #12
    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. Natan

    Static vs. non-static connection

    Natan, May 24, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    7,369
    Sami Vaaraniemi
    May 26, 2004
  2. Ben
    Replies:
    3
    Views:
    454
  3. Peter B. Steiger

    Can a static array contain a dynamic array of pointers?

    Peter B. Steiger, Apr 19, 2004, in forum: C Programming
    Replies:
    8
    Views:
    2,134
    Dave Thompson
    Apr 26, 2004
  4. Replies:
    2
    Views:
    1,609
    Jack Klein
    Jul 14, 2005
  5. Replies:
    23
    Views:
    870
    Chris Thomasson
    Aug 29, 2007
Loading...

Share This Page