<分区>
这样做之后,
char* example = (char*)malloc(10);
如何将第 5 个字节更改为“a”,以便 printf("%s", example);
会给我" a "
?
<分区>
这样做之后,
char* example = (char*)malloc(10);
如何将第 5 个字节更改为“a”,以便 printf("%s", example);
会给我" a "
?
最佳答案
你可以像下面这样简单地做:-
memset(example, ' ',10);
example[9] = 0; //assign end of string character equal to '\0'
example[4] = 'a';
它将如何运作? memset
将用空格填充所有字节并且 example[9] = 0;
将字符串结尾字符分配给它并且 example[4] = ' A';
将在第 5 个位置分配 'a'
。
关于在 C 中更改 malloc'd String 的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49141347/