Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > static array

Reply
Thread Tools

static array

 
 
dis_is_eagle@yahoo.com
Guest
Posts: n/a
 
      07-28-2006
hi...i am new to c programming....please explain me as to why should
an character array be declared as static...thanx...eric

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-28-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
lovecreatesbeauty
Guest
Posts: n/a
 
      07-28-2006

(E-Mail Removed) 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.

 
Reply With Quote
 
spibou@gmail.com
Guest
Posts: n/a
 
      07-28-2006
(E-Mail Removed) 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

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-28-2006
lovecreatesbeauty said:

>
> (E-Mail Removed) 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)
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      07-28-2006
lovecreatesbeauty wrote:
> (E-Mail Removed) 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
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      07-28-2006
"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.


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-28-2006
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)
 
Reply With Quote
 
lovecreatesbeauty
Guest
Posts: n/a
 
      07-28-2006

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.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-28-2006
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
static (non-zero) initialization of an array of structs (array of length #define LEN) anon.asdf@gmail.com C Programming 23 08-28-2007 11:54 PM
Convert Static Array to Dynamic Array ? raxitsheth@gmail.com C Programming 2 07-14-2005 01:25 AM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
Can a static array contain a dynamic array of pointers? Peter B. Steiger C Programming 8 04-26-2004 03:07 AM



Advertisments