Greg P. said:
| sets every element of the array of pointers to 0. Is
|
| TYPE xyz[NUMBER_OF_ELEMENTS] = {0};
|
| guaranteed to set every element of the arry to 0?
|
| TYPE means any valid type
I suggest using memset() and passing 0 as val. It is from string.h. It's
signature is:
void *memset(void *buf, int val, size_t count);
memset() will set every byte to 0x00. I don't think this is
necessarily reliable even if TYPE is an integer type (though it's
usually safe). I wouldn't use it for floating-point types or
pointers.
As long as you stick to character types it is perfectly legal to use
memset() like this (in both C89 and C99).
It is also legal in C99 to use this on an array of any integral type
of a specific bit size, i.e., int16_t, uint_32t, defined in
<stdint.h>, although it is not directly stated in the standard.
You must work it out from several different bits of the standard.
From 7.18.1.1 Exact-width integer types:
<quote>
The typedef name intN_t designates a signed integer type with width N,
no padding bits, and a two’s complement representation. Thus, int8_t
denotes a signed integer type with a width of exactly 8 bits.
The typedef name uintN_t designates an unsigned integer type with
width N. Thus, uint24_t denotes an unsigned integer type with a width
of exactly 24 bits.
<end quote>
Since there are no sign bits an any unsigned int type, all that there
can be are value and padding bits. Since the exact width types
specifically have no padding, every bit in the unsigned form is a
value bit and all bits 0 in any exact width unsigned type must be the
one and only representation of the value 0 for that type.
Next, from 6.2.6.2 on the representation of the integer types:
"For signed integer types, the bits of the object representation shall
be divided into three groups: value bits, padding bits, and the sign
bit. There need not be any padding bits; there shall be exactly one
sign bit. Each bit that is a value bit shall have the same value as
the same bit in the object representation of the corresponding
unsigned type"
So all bits 0 must be a valid representation of the value 0 in any
exact-width signed integer type as well.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq