c - Efficient way to fill integer array with short -


i'm trying fill array of unsigned integers (32-bit) short integers array (size not fixed). put short ints 1 after other in output array contains random values.

here code:

#define k_length (37) // arbitrary  void computeinput(unsigned short* input) {     unsigned int output[1000];     unsigned int i, j, gap;      j = 0;     gap = 0;     output[0] = 0;      (i = 0; < k_length; i++) {         output[j] |= (input[i] << gap);          if (gap) {             gap = 0;             j += 1;             output[j] = 0;         } else {             gap = 16;         }     }      // rest of output array set 0     (i = j, < 1000, i++) {         output[i] = 0;     }      // other stuff } 

first, part of algorithm checking gap value quite ugly don't how perform efficiently. second, don't know how ensure random values erased/replaced input values.

should set entire output array 0 before computing input values? seems inefficient.

i assume trying merge 16 bit unsigned integers pair-wise array of 32 bit unsigned integers:

#include <stdint.h>  void mergeshorts(size_t out_len, uint32_t output[out_len],             size_t in_len, const uint16_t input[in_len] ) {      size_t i;      // output must have enough entries.     assert( (in_len < size_max) && ((in_len + 1) / 2 <= out_len) );      ( = 0 ; < in_len / 2 ; += 1 )         output[i] = ((uint32_t)input[i / 2] << 16) | (input[i / 2 + 1];      // transfer last (odd) entry     if ( (i * 2) < in_len )         output[i++] = (uint32_t)input[in_len - 1] << 16;      // 0 rest of array     ( ; < out_len ; i++ )         output[i] = 0; } 

using stdint.h types guarantees proper sizes elements of both arrays , want.

if have fixed sizes, replace out_len , in_len constants. used output argument, rest of code missing. if not required, make local again (the name output implies expected caller).

normally, 1 pack function did , call function computeinput appropriate arguments:

mergeshorts(1000, output, k_length, input); 

Comments