试图对c中的结构数组使用realloc时使用的指针无效

试图对c中的结构数组使用realloc时使用的指针无效,c,arrays,pointers,realloc,C,Arrays,Pointers,Realloc,我的程序可以编译,但我不能正确使用指针和realloc。我试过看其他的例子,但我似乎无法把它翻译成我自己的程序。该程序的目的是从文件中读取单词,如果单词出现多次,则增加计数。一旦结构数组经过我的基底(5),我想重新分配空间,复制数组,然后添加下一个单词 任何帮助都将不胜感激 #include <stdio.h> #include <string.h> #include <stdlib.h> #define BASE 5 #define MAX 50 type

我的程序可以编译,但我不能正确使用指针和realloc。我试过看其他的例子,但我似乎无法把它翻译成我自己的程序。该程序的目的是从文件中读取单词,如果单词出现多次,则增加计数。一旦结构数组经过我的基底(5),我想重新分配空间,复制数组,然后添加下一个单词

任何帮助都将不胜感激

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BASE 5
#define MAX 50

typedef char *string;

 struct wordCount 
 {
 string word; 
 unsigned int count;
  }; 

 int main (void)
 {  
unsigned int i; 
unsigned int incremented; 
unsigned int j; 
char temp [40];  
struct wordCount wordArray[BASE]; 
struct wordCount *holder;  
FILE *infile;   

j = 0; 
infile = fopen("input.txt","r");
while (fscanf(infile, "%s", temp) == 1) { 
    incremented = 0; 
    for (i = 0; i < j; i++){
        if(strcmp(temp,wordArray[i].word) == 0){
            wordArray[i].count++;
            incremented++; 
        } 
     }
     if (incremented == 0){
        if (j<BASE){     
            wordArray[j].word = (char *)malloc((strlen(temp)+1) * 
                               sizeof(char));
            strcpy(wordArray[j].word,temp); 
            wordArray[j].count = 1; 
            j++;  
        } else {
          holder = realloc(wordArray, sizeof(wordArray) +1);
          *wordArray = *holder;
          wordArray[j].word = (char *)malloc((strlen(temp)+1) * sizeof(char));
          strcpy(wordArray[j].word,temp); 
          wordArray[j].count = 1; 
          j++; 
        }
     }
}

fclose(infile);

/* bring in next file*/
/*delete du plicates */ 
/*sort*/ 

for (i = 0; i < j; i++) {
    printf("%s ", wordArray[i].word);
    printf("%d\n", wordArray[i].count); 
}
/* and when done:*/ 
   for(i = 0; i < j; i++){
      free(wordArray[i].word);
}

return 0; 
 }
#包括
#包括
#包括
#定义基数5
#定义最大值50
typedef char*字符串;
结构字数
{
字符串字;
无符号整数计数;
}; 
内部主(空)
{  
无符号整数i;
无符号整数递增;
无符号整数j;
煤焦温度[40];
结构字计数字数组[BASE];
结构字计数*持有者;
文件*填充;
j=0;
infle=fopen(“input.txt”,“r”);
而(fscanf(infle,“%s”,temp)==1){
递增=0;
对于(i=0;i如果(j这里有一个最明显的错误:

holder = realloc(wordArray, sizeof(wordArray) +1);
请注意以下内容中的这一行:

void*realloc(void*ptr,size\u t size);

除非ptr为NULL,否则它必须是通过先前调用malloc()、calloc()或realloc()返回的

您的
wordArray
是一个静态分配的数组,它不是通过
malloc()
或friends动态分配的