Menu

Const suggestion for a mutable array

2021-11-14
2021-11-15
  • Nikita Leontiev

    Nikita Leontiev - 2021-11-14

    cppcheck 2.6 suggests to use const for num array in the code below:

    #include <inttypes.h>
    #include <stdio.h>
    #include <sstream>
    
    uint32_t str_to_ip(const char * ip_str)
    {
        uint16_t num[4];
        std::istringstream ss(ip_str);
        char ch;
        ss >> num[3] >> ch >> num[2] >> ch >> num[1] >> ch >> num[0];
        return (num[3] & 0xFF) << 24 | (num[2] & 0xFF) << 16 | (num[1] & 0xFF) << 8 | (num[0] & 0xFF);
    }
    
    int main()
    {
        printf("%u\n", str_to_ip("127.0.0.1"));
        return 0;
    }
    
    [test.cpp:7]: (style) Variable 'num' can be declared with const
    
     
  • CHR

    CHR - 2021-11-15

    Have you tried with current head? I can't reproduce it there.

     
  • Nikita Leontiev

    Nikita Leontiev - 2021-11-15

    Yes, issue not reproduced with cppcheck from main branch (hash: 4f9a563570980a2022785b90825c8ea7690b2cbb)

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.