Very new to C so please bear with me! I’m trying to read 3 arguments separated by spaces from “instruction” into the 3 strings using sscanf. I’ve got the following code so far:
char * buffer;
buffer = (char*) malloc (5000);
sscanf(instruction, "%s %s %s", &opcode, &arg1, &arg2, buffer);
However whilst it compiles fine, there is a seg fault on the last line where the sscanf is. Last time I saw this error it was because there wasn’t enough memory in the variables to store the information. I’m guessing I’m not using malloc properly?
I can’t post the entire code unfortunately as it doesn’t belong to me.
You are using
sscanfimproperly. Try this:Depending on how you use these variables, it might be better to allocate them on the stack: