First version: (Works)
//Using sprintf
int index = 56;
char filename[64], * suffix = "txt";
sprintf(filename, "log_%d.%s", index, suffix);
lr_output_message ("The new file name is %s", filename);
//This works
Second version: (Does not work)
//Using sprintf
int index = 56;
char *filename, * suffix = "txt";
sprintf(filename, "log_%d.%s", index, suffix);
lr_output_message ("The new file name is %s", filename);
//Fails with invalid parameter passed to function
In the second case you have to allocate space in memory for filename, using malloc()
If you do not allocate space you will have
undefined behaviour
because of you are writing to a not initialized pointer.Remember to always check the return of malloc:
Once you allocate memory and the usage is done. you've to release the allocated memory using free()