Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress "output truncated before terminating nul"

Tags:

c

gcc

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?

like image 856
AndreKR Avatar asked Sep 03 '25 06:09

AndreKR


1 Answers

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);
like image 173
dbush Avatar answered Sep 04 '25 20:09

dbush