c - 将字符指针的值赋给字符数组

标签 c arrays

我正在使用 strtok我的目标是将字符串划分为更小的标记,然后将这些单独的标记分配给字符数组或字符串。这是我的代码:

int main(void)
{
   char whole[100] = "Please help me";
   char *token;
   char individual[100];

   token = strtok(whole, " ");
   individual = token;   //I don't know what code fits in here
}

有人可以帮我吗?

最佳答案

#include <stdio.h>
#include <string.h>

int main(void)
{
    char whole[100] = "Please help me";
    char *token;
    char *individual[50];
    int i=0, n;

    token = strtok(whole, " ");
    while(token != NULL){
        individual[i++] = token;//just store pointer or make clone = strdup(token);
        token = strtok(NULL, " ");
    }
    n = i;
    for(i = 0; i < n; ++i){
        puts(individual[i]);
    }
    return 0;
}
<小时/>
char whole[100] = "Please help me";
char *token;
char individual[50][100];
int i=0, n;

token = strtok(whole, " ");
while(token != NULL){
    strcpy(individual[i++], token);//copy to char array
    token = strtok(NULL, " ");
}
n = i;
for(i = 0; i < n; ++i){
    puts(individual[i]);
}

关于c - 将字符指针的值赋给字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30043059/

相关文章:

c - 在 C 中将指针传递给结构体?

c++ - 用 Visual Studio 2005 编译 C 程序?

python - 将一个列表中的每个项目添加到另一个列表中的每个项目到第三个列表

arrays - 惰性属性中的 "Instance member cannot be used on type"错误

arrays - 高效堆叠和连接 NumPy 数组

c - 调用 execve( man, args, env ) 时出现 execve 错误

c - 需要帮助编写制作表格的程序

C内存分配,释放后覆盖

.net - 替换 for...if 数组迭代

arrays - 对数组应用过滤器后的 xor