我需要将来自用户的输入存储到字符串数组中。strcpy()和字符串数组
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char *history[10] = {0};
int main (void) {
char input[256];
input = "input";
strcpy(history[0], input);
return (EXIT_SUCCESS);
}
终端,我收到了段故障,并在NetBeans中,我得到的main.c上运行它:11:错误:不兼容的类型分配。我也尝试移动所有历史记录以将最新输入存储到第一个位置(历史[0])。
history[9] = history[8];
history[8] = history[7];
history[7] = history[6];
history[6] = history[5];
history[5] = history[4];
history[4] = history[3];
history[3] = history[2];
history[2] = history[1];
history[1] = history[0];
history[0] = input;
但是,这会导致这样的输出。
如果输入是 “输入”
历史0:输入 历史1:空 等
如果然后输入是 “新”
历史0:新 历史1:新 历史2:空 等
每次输入新的输入指向字符串转换的指针,但它只会导致最新的VA将被保存在历史数组中。