EEVblog® Electronics Community Forum
Electronics => Projects, Designs, and Technical Stuff => Topic started by: ricko_uk on October 31, 2021, 11:17:35 pm
-
Hi,
what is the official/guaranteed behaviour for initializing an array of size of X but with less elements?
Like:
uint8_t array[100] = { 0x11, 0x22, 0x33 };
and the case of a multi-dimensional array if different (just in case).
uint8_t array[2][100] = {{0x11, 0x22, 0x33},{0x11, 0x22, 0x33}};
I tested it and the remaining is initialised to 0 but not sure if that is the official C standard or if it is just the compiler I am using.
UPDATE - I found the answer. the remaining is automatically initialised to 0
Thank you :)
-
Programming questions placed in the programming forum (https://www.eevblog.com/forum/programming/) are more likely to attract the attention of programmers...
-
Thank you Mike, :)
I thought so too, but when I was doing that long time ago I rarely had replies and ended up deleting the posts and reposting here where I did get them. It seems that this forum is much more active also for code. :o :)
-
Since C99, you can also use array designators (designated initializers). For example,
unsigned char typemap[UCHAR_MAX + 1] = {
[' '] = WHITESPACE,
['a'] = LETTER | LOWERCASE,
['A'] = LETTER | UPPERCASE,
['0'] = DIGIT,
};
i.e. instead of just value you can use [index] = value . Like in C enums, any consecutive values without a designator get assigned to increasing elements after the last used designator.
Finally, if you have an array of structures, say
struct ivec2d {
int x;
int y;
};
struct ivec2d points[5] = { [0].x = 1, [0].y = -1, [3].x = 2, [4].y = -3 };
you can use [index].member = value in whatever order to initialize the array.
All uninitialized values get initialized to zero, and if defined multiple times, the last one wins.
The [first ... last] = value form is a GNU extension, and is only available in some C compilers (GCC, Clang, Intel CC).
-
Thank you Nominal Animal,
all clear, interesting and very useful!! Apart form the first code snippet:
unsigned char typemap[UCHAR_MAX + 1] = {
[' '] = WHITESPACE,
['a'] = LETTER | LOWERCASE,
['A'] = LETTER | UPPERCASE,
['0'] = DIGIT,
};
would the [' '] be converted to index 32 and ['a'] to index 97?
But then what are WHITESPACE, LETTER | LOWERCASE etc? Are the #defines you defined? If so what is the point, for example, of [' '] = WHITESPACE? Wouldn't that be the same as writing [' '] = ' '?
Thank you :)
-
would the [' '] be converted to index 32 and ['a'] to index 97?
Yes.
Well, technically it depends on the execution character set, so I am assuming your dev environment assumes the binary will use ASCII, UTF-8, ISO-8859-1/15, Windows-1252, or one of the compatible character sets, as they usually do. If you use a non-Western locale, it might not be true.
But then what are WHITESPACE, LETTER | LOWERCASE etc? Are the #defines you defined?
They could be preprocessor macros, yes, or enums, or both.
Since the array contains 8-bit values, a typical choice would be to use "magic cookies", i.e. bit masks:
enum {
WHITESPACE = 1 << 0,
LETTER = 1 << 1,
DIGIT = 1 << 2,
LOWERCASE = 1 << 3,
UPPERCASE = 1 << 4,
/* Still 3 bits unused! */
};
This means that if you wanted to implement your own isspace(), isdigit(), islower(), isupper(), isalpha() and isalnum() functions, initialize that array properly, then
#if defined(__GNUC__) || defined(__clang__)
#define STATIC_INLINE __attribute__((__const__, __unused__)) static inline
#else
#define STATIC_INLINE static inline
#endif
STATIC_INLINE int my_isspace(const unsigned char c) { return typemap[c] & WHITESPACE; }
STATIC_INLINE int my_isdigit(const unsigned char c) { return typemap[c] & DIGIT; }
STATIC_INLINE int my_islower(const unsigned char c) { return typemap[c] & LOWERCASE; }
STATIC_INLINE int my_isupper(const unsigned char c) { return typemap[c] & UPPERCASE; }
STATIC_INLINE int my_isalpha(const unsigned char c) { return typemap[c] & LETTER; }
STATIC_INLINE int my_isalnum(const unsigned char c) { return typemap[c] & (LETTER | DIGIT); }
would do the trick. In C, zero is false, and nonzero is true, so the tests simply return the specified bit(s) of the type map for the given character. If the bit(s) are (all) zero, then the result is false; otherwise the result is true. If you want a 0/1 result, use !!(typemap[c] & ...).
(! just is logical not: !x is true if and only if x is zero; otherwise it is false and has integer value 1. Thus, !!x 0 if and only if x is 0, otherwise it is 1.)
-
Thank you Nominal Animal, all clear now! :)