String Padding in C

前端 未结 10 1518
迷失自我
迷失自我 2020-11-28 22:25

I wrote this function that\'s supposed to do StringPadRight(\"Hello\", 10, \"0\") -> \"Hello00000\".

char *StringPadRight(char *string, int padded_len, char          


        
10条回答
  •  醉梦人生
    2020-11-28 22:45

    One thing that's definitely wrong in the function which forms the original question in this thread, which I haven't seen anyone mention, is that it is concatenating extra characters onto the end of the string literal that has been passed in as a parameter. This will give unpredictable results. In the example call of the function, the string literal "Hello" will be hard-coded into the program, so presumably concatenating onto the end of it will dangerously write over code. If you want to return a string which is bigger than the original then you need to make sure you allocate it dynamically and then delete it in the calling code when you're done.

提交回复
热议问题