Array initialization with values less than array size - Page 1
Forum section: electronics / projects

author: ricko_uk
author_rank: Super Contributor
timestamp: October 31, 2021, 11:17:35 pm
content: Hi,what is the official/guaranteed behaviour for initializing an array of size of X but with less elements?Like:uint8_t array = { 0x11, 0x22, 0x33 };and the case of a multi-dimensional array if different (just in case).uint8_t array = {{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 0Thank you

author: mikerj
author_rank: Super Contributor
timestamp: November 01, 2021, 12:06:04 am
content: Programming questions placed in theprogramming forumare more likely to attract the attention of programmers...

author: ricko_uk
author_rank: Super Contributor
timestamp: November 01, 2021, 12:07:38 am
content: 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.

author: Nominal Animal
author_rank: Super Contributor
timestamp: November 01, 2021, 12:10:27 am
content: Since C99, you can also use array designators (designated initializers). For example,Code:unsigned char typemap[UCHAR_MAX + 1] = {[' '] = WHITESPACE,['a'] = LETTER | LOWERCASE,['A'] = LETTER | UPPERCASE,['0'] = DIGIT,};i.e. instead of justvalueyou can use =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, sayCode:struct ivec2d {int x;int y;};struct ivec2d points = { .x = 1, .y = -1, .x = 2, .y = -3 };you can use.member=valuein 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] =valueform is a GNU extension, and is only available in some C compilers (GCC, Clang, Intel CC).

author: ricko_uk
author_rank: Super Contributor
timestamp: November 01, 2021, 12:26:51 am
content: Thank you Nominal Animal,all clear, interesting and very useful!! Apart form the first code snippet:Code: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

author: Nominal Animal
author_rank: Super Contributor
timestamp: November 01, 2021, 01:27:15 am
content: Quote from: ricko_uk on November 01, 2021, 12:26:51 amYes.Well, technically it depends on theexecution 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.Quote from: ricko_uk on November 01, 2021, 12:26:51 amThey 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:Code: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 ownisspace(),isdigit(),islower(),isupper(),isalpha()andisalnum()functions, initialize that array properly, thenCode:#if defined(__GNUC__) || defined(__clang__)#define STATIC_INLINE __attribute__((__const__, __unused__)) static inline#else#define STATIC_INLINE static inline#endifSTATIC_INLINE int my_isspace(const unsigned char c) { return typemap & WHITESPACE; }STATIC_INLINE int my_isdigit(const unsigned char c) { return typemap & DIGIT; }STATIC_INLINE int my_islower(const unsigned char c) { return typemap & LOWERCASE; }STATIC_INLINE int my_isupper(const unsigned char c) { return typemap & UPPERCASE; }STATIC_INLINE int my_isalpha(const unsigned char c) { return typemap & LETTER; }STATIC_INLINE int my_isalnum(const unsigned char c) { return typemap & (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 & ...).(!just is logical not:!xis true if and only ifxis zero; otherwise it is false and has integer value 1. Thus,!!x0 if and only ifxis 0, otherwise it is 1.)

author: ricko_uk
author_rank: Super Contributor
timestamp: November 01, 2021, 01:31:56 am
content: Thank you Nominal Animal, all clear now!