Home > Blockchain >  Suppress "output truncated before terminating nul"
Suppress "output truncated before terminating nul"

Time:10-13

I'm building a binary structure that has to include a certain string character sequence. To set the character sequence, I'm using

struct {
    char preamble[6];
    uint8_t check;
} msg;

strncpy(msg.preamble, "abcdef", 6);

This gives me a warning:

src\main.cpp:41:9: warning: 'char* strncpy(char*, const char*, size_t)' output truncated before terminating nul copying
6 bytes from a string of the same length [-Wstringop-truncation]

I'd like to keep the build log free of warnings, so that I can see actual issues quicker.

How can I fix/suppress this warning?

CodePudding user response:

If what you have isn't a string, don't treat it as such, i.e. don't use strncpy. Use memcpy instead.

memcpy(msg.preamble, "abcdef", 6);
  •  Tags:  
  • cgcc
  • Related