How to copy a string into a char array in C++ without going over the buffer

前端 未结 11 975
Happy的楠姐
Happy的楠姐 2020-12-08 03:13

I want to copy a string into a char array, and not overrun the buffer.

So if I have a char array of size 5, then I want to copy a maximum of 5 bytes from a string in

11条回答
  •  生来不讨喜
    2020-12-08 03:41

    void stringChange(string var){
    
        char strArray[100];
        strcpy(strArray, var.c_str()); 
    
    }
    

    I guess this should work. it'll copy form string to an char array.

提交回复
热议问题