generating *char buffer

I need to create a buffer that contains 300 symbols and pass it to the function described below:

void printThis(char *info);

What is the best way to generate this buffer?

If this is good:

char *buffer = new char()

then, how to add characters to this buffer? The following method is not good and it raises an access violation:

for (int i=0; i<300;i++)
{
   sprintf(s,"%s",'a');
}

Answers


std::vector<char> buffer(300, 'a');

// I'm guessing that printThis wants a zero-terminated string
buffer.push_back(0);

printThis(&buffer[0]);

If you're using the C++11 library, then buffer.data() might look nicer than &buffer[0].

If you really want to manage the memory yourself (hint: you don't), either create an automatic array:

char buffer[300];

or a dynamic array:

char * buffer = new char[300];

// Don't forget to delete it, and hope than nothing threw an exception
delete [] buffer;

Need Your Help

How to make a program to have its own version of PATH env

c# .net

I want to know if I can have custom version of PATH env variable specific to my executable?

Why is my new schema not showing in the table properites pane?

sql-server schema ssms

I am using SQL Server 2008 Express and Sql Server Management Studio 2008.

PHP multidimensional array key value

php arrays multidimensional-array

I am working with multidimensional arrays;

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.