fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct {
  6. char **args; /* An array of strings*/
  7. } asmInstruction;
  8.  
  9. int countTokens(char *charLine){
  10. return 3;//mock
  11. }
  12.  
  13. asmInstruction *tokenizeLine(char *charLine) {
  14. int words = countTokens(charLine);
  15. char *tokens;
  16.  
  17. asmInstruction *instr = (asmInstruction*) malloc(sizeof(asmInstruction));
  18. instr->args = (char**) malloc((words+1) * sizeof(char*));//+1 for NULL, or add member E.g instr->numOfWords = words
  19.  
  20. int count = 0;
  21. tokens = strtok(charLine, " ,");
  22. while (tokens) {
  23. instr->args[count] = malloc(strlen(tokens)+1);
  24. strcpy(instr->args[count++], tokens);
  25. tokens = strtok(NULL, " ,");
  26. }
  27. instr->args[count] = NULL;
  28.  
  29. return instr;
  30. }
  31.  
  32. int main(void) {
  33. char s[] = "ldr r2,r1";
  34. asmInstruction *instr = tokenizeLine(s);
  35. //test print
  36. for(int i=0; instr->args[i]; ++i)
  37. printf("%s\n", instr->args[i]);
  38. //...
  39. return 0;
  40. }
Success #stdin #stdout 0s 2184KB
stdin
Standard input is empty
stdout
ldr
r2
r1