I don’t understand why I can’t directly strtok(argv[1], ";")
(whould there be a difference if the argv[1]
is indeed an input from terminal with argv[1]
is actually a list on heap?)
char *multiDecimalToBinary(char *argv[], int count) {
int maxLen = 80 * (count + 1); // Max memory needed for result in this situation
char *result = malloc(maxLen * sizeof(char));
if (result == NULL) {
return NULL;
}
result[0] = '\0'; // Initialize the result string
/// why I need to copy argv[1] here?
/// I think if run through terminal, argv might be an static val,
/// but during the test, I pass in char *test = malloc(...)
char *s = malloc((strlen(argv[1]) + 1) * sizeof(char));
strcpy(s, argv[1]);
char *type = strtok(s, ";");
// printf("%s\n",argv[1]);
// char *type = strtok(argv[1], ";");
// printf("here\n");
void testMultiDecimalToBinary(void) {
char **test = malloc(5 * sizeof(char *));
test[1] = "{char;int;unsigned char}";
test[2] = "7";
test[3] = "10000000";
test[4] = "255";
assert(strcmp(multiDecimalToBinary(test, 2),
"0000 0111 0000 0000 1001 1000 1001 0110 1000 0000 1111 1111") == 0);
free(test);
}
I thought both of this two method should work, but only one can
You need to sign in to view this answers