how to initialize a char array?

前端 未结 8 1845
名媛妹妹
名媛妹妹 2020-12-14 14:39
char * msg = new char[65546];

want to initialize to 0 for all of them. what is the best way to do this in C++?

8条回答
  •  误落风尘
    2020-12-14 15:06

    The C-like method may not be as attractive as the other solutions to this question, but added here for completeness:

    You can initialise with NULLs like this:

    char msg[65536] = {0};
    

    Or to use zeros consider the following:

    char msg[65536] = {'0' another 65535 of these separated by comma};
    

    But do not try it as not possible, so use memset!

    In the second case, add the following after the memset if you want to use msg as a string.

    msg[65536 - 1] =  '\0'
    

    Answers to this question also provide further insight.

提交回复
热议问题