Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 相同的字符串存储在数组的所有元素中_C_Arrays_String - Fatal编程技术网

C 相同的字符串存储在数组的所有元素中

C 相同的字符串存储在数组的所有元素中,c,arrays,string,C,Arrays,String,我试图将不同的字符串保存到char*数组中,但出于某种原因,它将相同的字符串保存到数组的每个元素中,即使我将不同的字符串存储到元素中 char string[99]; char line[99]; FILE* out = fopen("data.out", "w"); char *words[999]; int wcount = 0; while(fscanf(fpointer, "%s", string) != EOF) { if((strlen(string) + strlen

我试图将不同的字符串保存到char*数组中,但出于某种原因,它将相同的字符串保存到数组的每个元素中,即使我将不同的字符串存储到元素中

char string[99];
char line[99];
FILE* out = fopen("data.out", "w");
char *words[999];
int wcount = 0;


 while(fscanf(fpointer, "%s", string) != EOF)
{
    if((strlen(string) + strlen(line))-1 <= number)
    {
        strcat(line, string);
        char word[99];
        strcpy(word, string);
        words[wcount] = word;
        printf("should have saved %s at %d\n", word, wcount);
        wcount++;

        if(strlen(line) < number)
        {
            strcat(line, " ");
        }
        puts(line);
    }
    else
    {
        fprintf(out,"%s", line);
        fprintf(out, "\n");
        strcpy(line, string);
        strcat(line, " ");
    }
}

fprintf(out, "%s", line);

printf("wcount is %d\n", wcount);
puts(words[0]);
puts(words[1]);
puts(words[2]);
char字符串[99];
字符行[99];
FILE*out=fopen(“data.out”,“w”);
字符*字[999];
int wcount=0;
while(fscanf(fpointer,“%s”,字符串)!=EOF)
{

如果((strlen(string)+strlen(line))-1也许你想要这样的东西? 现在无法测试它

 char **words = NULL;
 while(fscanf(fpointer, "%s", string) != EOF)
{
    if((strlen(string) + strlen(line))-1 <= number)
    {
        strcat(line, string);
        words = realloc(words,sizeof(char *)*(wcount+1));
        words[wcount] = malloc(sizeof(char)*(strlen(string)+1));
        strcpy(words[wcount], string);
        printf("should have saved %s at %d\n", words[wcount], wcount);
        wcount++;

        if(strlen(line) < number)
        {
            strcat(line, " ");
        }
        puts(line);
    }
    else
    {
        fprintf(out,"%s", line);
        fprintf(out, "\n");
        strcpy(line, string);
        strcat(line, " ");
    }
}

fprintf(out, "%s", line);

printf("wcount is %d\n", wcount);
puts(words[0]);
puts(words[1]);
puts(words[2]);
char**words=NULL;
while(fscanf(fpointer,“%s”,字符串)!=EOF)
{

如果在此语句中((strlen(string)+strlen(line))-1,则单词[wcount]=word;数组单词的所有元素都包含相同的地址(第一个字符)局部变量word。因此代码没有意义,而且有未定义的行为。您使用什么版本的C进行编译?所有指针都指向相同的
word
,因此它们没有区别。(当您试图在末尾打印它时,
word
甚至不再存在)。您必须将单词复制到某个存储中,而不仅仅是设置指向它的指针。几千次重复:(这不起作用,只是说“-bash:./wordproc.c:权限被拒绝”。但最初我有char*words[999];而不是char**words=NULL;我忘了把它放在我原来的问题上。c文件不是可执行文件。你是不是打算运行。/a.out(假设你已经编译了它)?你可能想使用我的解决方案,动态分配数组,这样你就可以存储无限多的单词。