在C中创建n个字符串的最快方法是什么

0 c string

什么是创建重复字符串的最快/最短方式.

例如, n = 10, char = '*', resulting allocated string: **********

Dav*_*Yaw 11

使用memset.

int n = 10;
char c = '*';

char* buf = malloc(n+1);
memset(buf, c, n);
buf[n] = '\0';

free(buf);
Run Code Online (Sandbox Code Playgroud)


R..*_*R.. 6

memset(buf, '*', 10); buf[10]=0;

替换'*'10使用您想要的值,如果事先不知道长度并且可能很大,请使用buf=malloc(n+1);获取缓冲区.