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

I am new to C an am having some trouble 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" */

Answers


You can try:

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

Need Your Help

How to use case sensitivity in instr() in MySQL?

mysql select case-sensitive

Possible duplicate: How to apply wildcard in instr() in MySQL?

Replace term with a variable in Prolog

prolog dcg

I have a DCG in Prolog that I can query like so:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.