How to add a char/int to an char array in C?

后端 未结 5 880
感情败类
感情败类 2020-12-14 02:58

How can I add \'.\' to the char Array := \"Hello World\" in C, so I get a char Array: \"Hello World.\" The Question seems simple but I\'m struggling.

Tried the follo

5条回答
  •  生来不讨喜
    2020-12-14 03:15

    Suggest replacing this:

    char str[1024];
    char tmp = '.';
    
    strcat(str, tmp);
    

    with this:

    char str[1024] = {'\0'}; // set array to initial all NUL bytes
    char tmp[] = "."; // create a string for the call to strcat()
    
    strcat(str, tmp); // 
    

提交回复
热议问题