c - Sprintf fails with invalid parameter passed to function in loadrunner while defining string using *varname -
this question has answer here:
- what difference between char s[] , char *s? 12 answers
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 %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 %s", filename); //fails invalid parameter passed function
your first version (is correct and) works, because there (enough) memory allocated
filename
,char
array.char filename[64]. //....
your second version (is incorrect and) not work, because there no memory allocation
filename
, define pointer.char *filename,//...
this not allocate memory pointer automatically.
in other words, pointer not pointing valid memory address. so, trying access memory pointed pointer invalid , invokes undefined behaviour.
solution: need allocate memory pointer before use it. can use malloc()
, family functions allocate memory pointer.
also, 1 allocate memory , usage done. you've release allocated memory using free()
Comments
Post a Comment