Sizeof vs Strlen

前端 未结 4 830
北荒
北荒 2020-11-29 02:49
#include 
#include 

int main(int argc, char *argv[]) {
    char string[] = \"october\"; // 7 le         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 03:12

    You must eliminate buffer overflow problem in this example. One way to do this - is to use strncpy:

    memset(string, 0, sizeof(string));
    strncpy(string, "september", sizeof(string)-1);
    

提交回复
热议问题