Actually I have a string looking like this : "1,22,333,,55555,666666,7777777"
From this string I have created two array :
- One with the size of each parameter. For the given exemple it looks like this :
[1;2;3;0;5;6;7]
- The second with the pointer that reference the beginning of each parameter. For the given exemple it looks like this :
["1,22,333,,55555,666666,7777777";
"22,333,,55555,666666,7777777";
"333,,55555,666666,7777777";
",55555,666666,7777777";
"55555,666666,7777777";
"666666,7777777";
"7777777"]
The parameters can change in length and in type but not in order. So i want to store them with typecasting for those concerned
I have tested to sscanf
each index of the pointer array but it seems not working
char* format = strcat(strcat("%", params_sizes[0]), "u");
strutest.un = sscanf(params_pointer[0], format);
Do you have any idea that can help me ?
CodePudding user response:
This line
char* format = strcat(strcat("%", params_sizes[0]), "u");
is wrong.
The prototype for strcat
is
char *strcat(char *restrict dest, const char *restrict src);
where dest
is the destination, i.e. (a pointer to) the object where the concatenated string is stored.
So looking at your code:
strcat("%", params_sizes[0]
^^^
dest !!!
you are trying to store the result in a string literal. That won't work (modifying a string literal in undefined behavior).
You need something like:
char format[1024] = "%"; // Sufficiently big char array that can be modified
strcat(strcat(format, params_sizes[0]), "u");
BTW: Also consider sprintf
instead of nested strcat