I was looking at a project and came across the following code and am unable to figure out what the sprintf is doing in this context and was hoping someone might be able to help me figure it out.
int loc = 0;
int size = 35;
sprintf(storage+(loc),"A"); //Don't know what this does
sprintf(storage+(loc),"%i", size); //Don't know what this does
sprintf(storage+(loc), "%i", start); //Don't know what this does
start += size;
loc += 3;
int index = 0;
for(int j = index+1; j < index+4; j++)
value += storage[j];
iss >> actVal; //Don't understand how this now contains size
Ugly code! Regardless, for the first part,
&storage[loc]. You end up with a string
start = 1234, or in long form:
sprintf(&storage,"A"); sprintf(&storage,"%i", size); sprintf(&storage, "%i", start);
For the second part, assuming we have the
"A35\0<unknown_value>1234\0" above, we get:
value += '3'; value += '5'; value += '\0'; value += '<unknown_value>'; // This might technically be undefined behaviour
value = "35".
iss.str(value); iss >> actVal;
This turns the string into an input stream and reads out the first string representing an integer,
"35", and converts it into an integer, giving us basically
actVal = atoi(value.c_str());.