0

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;    
 }
}
hippietrail
  • 15,848
  • 18
  • 99
  • 158
bitcruncher
  • 800
  • 1
  • 7
  • 14
  • 1
    Are you really reading strings ? If so,'buffer' should be char. If not, you shouldn't be using strncat – nos Jul 01 '10 at 07:52
  • +1 for that. they really aren't strings if nuls are rampant. switched to memcpy and problem solved. – bitcruncher Jul 03 '10 at 15:32

2 Answers2

1

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

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • 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. – Nietzche-jou Jul 01 '10 at 08:48
1
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
  • 2,512
  • 8
  • 26
  • 36