Author Topic: Array initialization with values less than array size  (Read 2546 times)

0 Members and 1 Guest are viewing this topic.

Offline ricko_ukTopic starter

  • Super Contributor
  • ***
  • Posts: 1233
  • Country: fr
Array initialization with values less than array size
« 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 :)

« Last Edit: November 01, 2021, 12:50:01 am by ricko_uk »
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3671
  • Country: gb
Re: Array initialization with values less than array size
« Reply #1 on: November 01, 2021, 12:06:04 am »
Programming questions placed in the programming forum are more likely to attract the attention of programmers...
 

Offline ricko_ukTopic starter

  • Super Contributor
  • ***
  • Posts: 1233
  • Country: fr
Re: Array initialization with values less than array size
« Reply #2 on: November 01, 2021, 12:07:38 am »
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 :)

« Last Edit: November 01, 2021, 12:09:22 am by ricko_uk »
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 8354
  • Country: fi
    • My home page and email address
Re: Array initialization with values less than array size
« Reply #3 on: November 01, 2021, 12:10:27 am »
Since C99, you can also use array designators (designated initializers).  For example,
Code: [Select]
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
Code: [Select]
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).
« Last Edit: November 01, 2021, 12:12:41 am by Nominal Animal »
 

Offline ricko_ukTopic starter

  • Super Contributor
  • ***
  • Posts: 1233
  • Country: fr
Re: Array initialization with values less than array size
« Reply #4 on: November 01, 2021, 12:26:51 am »
Thank you Nominal Animal,

all clear, interesting and very useful!! Apart form the first code snippet:

Code: [Select]
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 :)
« Last Edit: November 01, 2021, 12:45:01 am by ricko_uk »
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 8354
  • Country: fi
    • My home page and email address
Re: Array initialization with values less than array size
« Reply #5 on: November 01, 2021, 01:27:15 am »
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:
Code: [Select]
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
Code: [Select]
#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.)
 

Offline ricko_ukTopic starter

  • Super Contributor
  • ***
  • Posts: 1233
  • Country: fr
Re: Array initialization with values less than array size
« Reply #6 on: November 01, 2021, 01:31:56 am »
Thank you Nominal Animal, all clear now! :)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->