How to create a string of variable length with character X in C

cinitializationloopsstring

I am new to C and am having some troubles with strings. How do I create a string of variable length containing a specified character in C? This is what I have tried but I get a compiler error:

int  cLen     = 8    /* Specified Length    */ 
char chr      = 'a'; /* Specified Character */
char outStr[cLen];
int  tmp      = 0;
while (tmp < cLen-1)
  outStr[tmp++] = chr;

outStr[cLen-1] = '\0';

/* outStr = "aaaaaaaa" */

Best Answer

You can try:

char *str = malloc(cLen + 1);
memset(str, 'a', cLen);
str[cLen] = 0;