static array

D

dis_is_eagle

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

Ian Collins

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.
 
L

lovecreatesbeauty

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.
 
S

spibou

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
 
R

Richard Heathfield

lovecreatesbeauty said:
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?
 
F

Flash Gordon

lovecreatesbeauty said:
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.
 
O

osmium

Ian Collins said:
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.
 
R

Richard Heathfield

osmium said:
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.
 
L

lovecreatesbeauty

Flash said:
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.
 
L

lovecreatesbeauty

Richard said:
lovecreatesbeauty said:

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?
 
D

Dave Thompson

lovecreatesbeauty said:


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top