我有一個最多200個字符的字符數組。我想爲數組中的每個單詞指定一個數組。我有這張照片作爲應該發生的事例。 I am not allowed to post images so here is a link to the picture on imgur將字符數組中的指針指定給字符串中的每個字C
我試着循環查找空格的字符串,爲每個事件分配一個新的指針。但是,它每次都打印剩餘的字符串,然後崩潰。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[200];
char *arr[200];
fgets(str, 200, stdin);
arr[1] = &str[5];
printf("%s", arr[1]);
int i = 0;
int next = 0;
char ch = ' ';
for (; i < 200; i++) {
ch = str[i];
if (ch == ' '){
arr[next] = &str[i];
next++;
}
}
i = 0;
for (; i < 200; i++) {
printf("%s", arr[i]);
}
return 0;
}
發佈您的代碼。 – 2014-09-25 22:02:26
此外,它看起來像'strtok'可能在這種情況下有用。 – dreamlax 2014-09-25 22:02:43
我們無法處理我們看不到的代碼。 – 2014-09-25 22:04:36