I used strtok to tokenize an array. I wanted to store the char pointer, that strtok returns, into an array.
char exp[] = {"1000 + 1000"};
char operands[50];
p = strtok(exp, " ");
Now I wanted to store the value of p (that is 1000) into the operands[i] array. I tried it like this:
memcpy(&operands[i], p, 49);
But it only copies a single integer.
strcpy(operands, p)?std::stringis your friend.strtokdoes - put a NUL character in toexp[]after the first token, such that yourmemcpy(which has undefined behaviour because 49 is way larger thansizeof exp) copies "1000\0+ 1000\0" and about 40 other garbage characters. Listen to Bathsjeba - if you usestd::stringyou're less likely to screw up, and if you do you've a somewhat better chance to get the wrong string output instead of crashing your program.