Burne said:
Christian Bau said:
Malcolm said:
I want to fill char/int arrays with zeros.
Is something like this portable/save:
char myArray[30][80];
memset(myArray,0,(30*80) * sizeof(char));
sizeof(char) always equals one.
"char" plays two roles in C. A "char" is the basic storage unit;
everything is measured in units of "char", and therefore sizeof (char)
must be 1. On the other hand, "char" is also a type of its own. If I
need an array of integers, and I know that all values are from 0 to 127,
No, the range of char is -128 to 127, and 0 to 255 for unsigned char.
The range of `signed char' is at least -127 through +127,
possibly wider. The range of `unsigned char' is at least 0
through 255, possibly wider (although the minimum value will
necessarily be zero). The range of plain `char' is the same
as one or the other of `signed char' or `unsigned char', at
the whim of the implementation. Thus, the range 0 through 127
is exactly the set of values that can be guaranteed to fit in
a plain `char' on every C implementation, however bizarre.
To put it another way, any attempt to store a value outside
this range in a plain `char' is non-portable.[*]
[*] Of course, constructs like `char c1 = CHAR_MIN;' and
`char c2 = -1;' are "portable" in the sense that they
work everywhere. However, the values of `c1' and `c2'
are implementation-dependent, so the constructs are
"non-portable" in that they don't give the same
results everywhere.