P
pete
Martin said:I have an array as follows:
char unsigned data[N];
I want to set every single bit in the array to 1. My initial thoughts
were:
memset(data, UCHAR_MAX, sizeof data);
but then when I looked at the declaration for memset, I saw that the
middle parameter was an int rather than an unsigned char. The problem
here is that UCHAR_MAX isn't guaranteed to "fit" in an int.
Can anyone think of a way to set all the bits to 1 (other than looping
through the elements and assigning UCHAR_MAX to them)?
Possibly, what I need is an int value that will convert to UCHAR_MAX
on every implementation... hmm...
memset(data, -1, sizeof data);
((unsigned char)-1) equals UCHAR_MAX.
The int argument value is converted to unsigned char by memset.
N869
7.21.6.1 The memset function
Synopsis
[#1]
#include <string.h>
void *memset(void *s, int c, size_t n);
Description
[#2] The memset function copies the value of c (converted to
an unsigned char) into each of the first n characters of the
object pointed to by s.