简体   繁体   中英

string concatenation with strncat leads to error in signedness

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;    
 }
}

显然,在您的编译器中char是带signed char ,因此是警告消息。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM