views:

41

answers:

2

update: the point of whether char, signed char, or unsigned was ultimately moot here. it was more appropriate to use memcpy in this situation, since it works indiscriminately on bytes.

Couldn't be a simpler operation, but I seem to be missing a critical step. In the following code, I am attempting to fill bufferdata with buffer for which the compiler warns me of a difference in signedness.

unsigned char  buffer[4096] = {0};
char *bufferdata;

bufferdata = (char*)malloc(4096 * sizeof(bufferdata));

if (! bufferdata)
  return false;

while( ... )
{
    // nextBlock( voidp _buffer, unsigned _length );
 read=nextBlock( buffer, 4096);

 if( read > 0 )
 {

  bufferdata = strncat(bufferdata, buffer, read); // (help)
  // leads to: pointer targets in passing argument 2 of strncat differ in signedness.

  if(read == 4096) {

   // let's go for another chunk
   bufferdata = (char*)realloc(bufferdata, ( strlen(bufferdata) + (4096 * sizeof(bufferdata)) ) );
   if (! bufferdata) {
    printf("failed to realloc\n");
    return false;
   }

  }

 }
 else if( read<0 )
 {
  printf("error.\n");
  break;
 }
 else {
  printf("done.\n");
  break;    
 }
}
+1  A: 

Obviously in your compiler char is signed char, thus the warning message.

kgiannakakis
you should just use `char` if your semantic of it is a buffer of uniterpreted bytes. Reserve `signed` and `unsigned` `char` for values that have the semantic of numerical values.in addition, in case that is your real code, your allocation for `bufferdata` is way to large, probably you meant `sizeof(*bufferdata)`, or even better leave that `sizeof` out, your handling `char` buffers anyhow.
Jens Gustedt
That's wrong. You have to use `unsigned char` if you want uninterpreted bytes because only `unsigned char` and unsigned bitfields are guaranteed to be a pure binary representation.
Cirno de Bergerac
+1  A: 
char * strncat ( char * destination, char * source, size_t num );

so as your destination buffer is unsigned char so there will be a warning of sign. You can either change your buffer to char array if signed is not necessary else you can ignore warnings using -w option in compiler.

Kumar Alok