passing array of ints to functions which expects chars in C -


how can pass buffer of unsigned ints, function expecting buffer of unsigned chars? function operate , update buffer. next pseudo-code of trying achieve.

unsigned int* inputbuffer = (unsigned int*)malloc(buffersize);  function(inputbuffer); <- how perform correctly?  bool function(unsigned char *buffer) {   ...operate , update values of buffer } 

it depends on function does. if operates on bytes in buffer (like memcpy, memcmp etc. do), cast pointer:

function((unsigned char *)inputbuffer); 

if function operates on elements (integers) in buffer, you'll have copy entire contents temporary buffer:

size_t nelems = buffersize / sizeof(unsigned int); unsigned char *temp = malloc(nelems); if (temp == null)     // handle error  (size_t i=0; < nelems; i++)     temp[i] = (unsigned char)(inputbuffer[i]); function(temp);  // copy results inputbuffer free(temp); 

however, if find need this, there's design flaw somewhere in program. shouldn't have this, ever, unless you're working poorly designed library.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -