Newbie Question. How do you check if array is empty? and a variableis not assigned to anything?

C

chutsu

As the title says:
How do you check if array is empty?
And also how do you check that a variable is not assigned to
anything?
 
C

chutsu

chutsu said:


It depends what you mean by "empty". If you think "filled with zero
values" means "empty", check each value for 0 to determine whether
the array is empty. If you mean something else, what do you mean?


If you mean "how do you check that a variable has not been assigned
a value?", you don't. If a variable (or rather, an object) has not
been assigned a value, you're not allowed to look at it. All you
can do is assign to it. Therefore, you the programmer are supposed
to know whether an object has been assigned a value.

To make this easier for myself, I try to remember to initialise
*everything*, and consider the absence of such initialisation to be
a bug in my program. Not everyone agrees with me. In fact, there
are some very bright people in this very newsgroup who disagree
with me... but I have my reasons (historical, and painful).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Forged article? Seehttp://www.cpax.org.uk/prg/usenet/comp.lang.c/msgauth.php
"Usenet is a strange place" - dmr 29 July 1999

But what if your C program requires input from a user. How do you
check if the user has entered anything at all instead of just pressing
enter?
Thanks
Chris
 
S

Sjouke Burry

chutsu said:
As the title says:
How do you check if array is empty?

Check your program.
And also how do you check that a variable is not assigned to
anything?

No variable is ever assigned to anything.

It can be use for millions of purposes.
 
K

Keith Thompson

chutsu said:


It depends what you mean by "empty". If you think "filled with zero
values" means "empty", check each value for 0 to determine whether
the array is empty. If you mean something else, what do you mean?
[...]

But what if your C program requires input from a user. How do you
check if the user has entered anything at all instead of just pressing
enter?

That's a *very* different question, and it depends on how you're
reading the input. Richard Heathfield posted some good information.
You might also take a look at section 12 of the comp.lang.c FAQ,
<http://www.c-faq.com/>.
 
A

Antoninus Twink

If a variable (or rather, an object) has not been assigned a value,
you're not allowed to look at it. All you can do is assign to it.

Typical Heathfield bullshit.

If you have a static variable, then there is no undefined behavior
involved in "looking it at" or using it in any other way even before
you've assigned it a value.
 
W

Willem

Antoninus Twink wrote:
) On 2 Jun 2009 at 22:07, Richard Heathfield wrote:
)> If a variable (or rather, an object) has not been assigned a value,
)> you're not allowed to look at it. All you can do is assign to it.
)
) Typical Heathfield bullshit.
)
) If you have a static variable, then there is no undefined behavior
) involved in "looking it at" or using it in any other way even before
) you've assigned it a value.

That's because static variables are implicitly assigned zero values.
So they can't have not been assigned a value.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
A

Antoninus Twink

Antoninus Twink may be distinguishing between "initialization" and
"assignment".

Exactly so.

"The word "pedant" is used by people who don't care about correctness to
describe people who do." (R Heathfield, writing from a glass house
somewhere in England)
 

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,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top