c - 在 c 中使用标记化

标签 c pointers multidimensional-array strtok

我正在尝试标记一条线并将其放入二维数组中,到目前为止我已经想出了这个,但我觉得我还很遥远:

/**
 * Function to tokenize an input line into seperate tokens
 *
 * The first arg is the line to be tokenized and the second arg points to
 * a 2-dimentional string array. The number of rows of this array should be
 * at least MAX_TOKENS_PER_LINE size, and the number of columns (i.e., length
 * of each string should be at least MAX_TOKEN_SIZE)
 *
 * Returns 0 on success and negative number on failure
 */

int __tokenize(char *line, char tokens[][MAX_TOKEN_SIZE], int *num_tokens){

char *tokenPtr;
tokenPtr = strtok(line, " \t");
    for(int j =0; j<MAX_TOKEN_SIZE; j++){
      while(tokenPtr != NULL){
        if(!(tokens[][j] = tokenPtr)){return -1;}
            num_tokens++;
            tokenPtr = strtok(NULL, " \t");
        }
    }
  return 0;
}

最佳答案

int __tokenize(char *line, char tokens[][MAX_TOKEN_SIZE], int *num_tokens)
{
char *tokenPtr;
tokenPtr = strtok(line, " \t");
for (int i = 0; tokenPtr; i++)
{
            tokens[i] = tokenPtr;
            tokenPtr = strtok(NULL, " \t");
}
}

希望这能起作用。

关于c - 在 c 中使用标记化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5887394/

相关文章:

c - 关于比较指针和整数的警告

c - 如何修复C中的 ‘memory leak’

const 导致 "formal parameter different from declaration"警告

arrays - 为什么在使用指针访问未对齐的 uint16 数组时抛出 "alignment exception"而在使用下标访问数组时不抛出?

c++ - 如果变量名不跟在 char* 后面,const char* 是否有效?

c - 用户想要删除一个值,但该值不在二叉树上。该如何治疗呢?

javascript - 如何计算和乘以数组中的复选框行

c - 从文件中读取第一行给出尺寸的二维数组

使用循环比较 2 个链表

arrays - postgres,多维数组的包含运算符在比较之前执行展平?