C将字符串存储到数组中
我正在研究“C编程现代方法第二版”文本中的一个问题。我想写一个写最小和最大单词的程序。当用户输入4个字母的单词时,程序停止接受输入 我正在使用一个字符串数组来解决这个问题,但我甚至无法让我的程序在其中存储单词C将字符串存储到数组中,c,arrays,string,C,Arrays,String,我正在研究“C编程现代方法第二版”文本中的一个问题。我想写一个写最小和最大单词的程序。当用户输入4个字母的单词时,程序停止接受输入 我正在使用一个字符串数组来解决这个问题,但我甚至无法让我的程序在其中存储单词 #include <stdio.h> #include <string.h> #define WORD_LEN 20 int main() { char word[WORD_LEN]={0},ch; char *a[10]={};
#include <stdio.h>
#include <string.h>
#define WORD_LEN 20
int main()
{
char word[WORD_LEN]={0},ch;
char *a[10]={}; //Max 10 words in the array
int i=0,j;
for(;;)
{
printf("Enter a word: ");
fgets(word,WORD_LEN,stdin);
strtok(word, "\n"); //removes newline
a[i] = word;
if(strlen(word) == 4) //if word is 4 characters
break; //break out of loop
i++;
}
for(j=0;j<i;j++) //displaying array
printf("%s\n",a[j]);
return 0;
}
知道我做错了什么吗?谢谢。正如BLUEPIXY所提到的,您在所有a[i]中都存储了相同的地址。因此在循环结束时,它会打印最后一次输出i次 解决方案: 您需要为[i]分配内存并复制字符串
#include <stdio.h>
#include <string.h>
#define WORD_LEN 20
#define MAX_NUM_WORD 10 //Max 10 words in the array
int main()
{
char word[WORD_LEN]={0},ch;
char *a[MAX_NUM_WORD]={0};
int i=0,j;
for(;;)
{
printf("Enter a word: ");
fgets(word,WORD_LEN,stdin);
strtok(word, "\n"); //removes newline
a[i] = malloc(sizeof(char)* (strlen(word)+1)); //1 for '\0'
strcpy(a[i], word);
i++;
if(strlen(word) == 4) //if word is 4 characters
break; //break out of loop
//i++; //You will be missing last 4 letter word if i++ is here.
if(MAX_NUM_WORD <= i) //You can store only MAX_NUM_WORD strings
break;
}
for(j=0;j<i;j++) //displaying array
printf("%s\n",a[j]);
//Your other code.
for(i=0; i<MAX_NUM_WORD && NULL != a[i]; i++)
free(a[i]); //Free the allocated memory.
return 0;
}
#包括
#包括
#定义单词\u LEN 20
#定义最多10个单词//数组中最多10个单词
int main()
{
字符字[word_LEN]={0},ch;
char*a[MAX_NUM_WORD]={0};
int i=0,j;
对于(;;)
{
printf(“输入一个单词:”);
fgets(单词、单词、标准单词);
strtok(单词“\n”);//删除换行符
a[i]=malloc(sizeof(char)*(strlen(word)+1));//1表示“\0”
strcpy(a[i],单词);
i++;
if(strlen(word)==4)//如果word是4个字符
break;//断开循环
//i++;//如果i++在这里,您将丢失最后4个字母的单词。
如果(MAX_NUM_WORD添加到其他答案,当使用为字符串分配内存时,最好也检查从它返回的void*
指针的返回值
此外,检查的返回值也是安全的,只是为了超级安全
此解决方案演示了以下几点:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WORD_LEN 20
#define MAX_NUM_WORD 10
#define EXIT_LEN 4
int
main(void) {
char word[WORD_LEN];
char *a[MAX_NUM_WORD];
int i = 0, wrd;
while (i < MAX_NUM_WORD) {
printf("Enter a word: ");
if (fgets(word, WORD_LEN, stdin) != NULL) {
word[strlen(word)-1] = '\0';
}
a[i] = malloc(strlen(word)+1);
if (a[i] == NULL) {
fprintf(stderr, "%s\n", "Malloc Problem");
exit(EXIT_FAILURE);
}
strcpy(a[i], word);
i++;
if (strlen(word) == EXIT_LEN) {
break;
}
}
// Print and free, all at once.
for (wrd = 0; wrd < i; wrd++) {
printf("%s\n", a[wrd]);
free(a[wrd]);
a[wrd] = NULL;
}
return 0;
}
#包括
#包括
#包括
#定义单词\u LEN 20
#定义最大数量的单词10
#定义出口4
int
主(空){
字符词;
字符*a[MAX_NUM_WORD];
int i=0,wrd;
while(i
a[i]=word;
:您将word
的相同地址设置为a[i]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WORD_LEN 20
#define MAX_NUM_WORD 10
#define EXIT_LEN 4
int
main(void) {
char word[WORD_LEN];
char *a[MAX_NUM_WORD];
int i = 0, wrd;
while (i < MAX_NUM_WORD) {
printf("Enter a word: ");
if (fgets(word, WORD_LEN, stdin) != NULL) {
word[strlen(word)-1] = '\0';
}
a[i] = malloc(strlen(word)+1);
if (a[i] == NULL) {
fprintf(stderr, "%s\n", "Malloc Problem");
exit(EXIT_FAILURE);
}
strcpy(a[i], word);
i++;
if (strlen(word) == EXIT_LEN) {
break;
}
}
// Print and free, all at once.
for (wrd = 0; wrd < i; wrd++) {
printf("%s\n", a[wrd]);
free(a[wrd]);
a[wrd] = NULL;
}
return 0;
}