criedjoker 2015-07-08 08:00 采纳率: 80%
浏览 1547
已采纳

哪位大神帮我看看代码有什么问题

程序运行后,输入字符串没反应,小弟刚学C语言,先谢谢各位大神了。
代码如下:
#include
#include
#include
#define BUF_LEN 100
#define INIT_STR_EXT 50
#define WORDS_INCR 5
int main(void)
{
char delimiters[]=" \n\".,;:!?)(";
char buf[BUF_LEN];
size_t str_size=INIT_STR_EXT;
char* pStr=(char*)malloc(str_size);
pStr='\0';
printf("Enter some prose with up to %d characters per line.\nTerminate input by entering an empty line:\n",BUF_LEN);
while(true)
{
fgets(buf,BUF_LEN,stdin);
if(buf[0]=='\n')
break;
if(strlen(pStr)+strlen(buf)+1>str_size)
{
str_size=strlen(pStr)+strlen(buf)+1;
pStr=(char
)realloc(pStr,str_size);
}
strcat(pStr,buf);

}
size_t maxWords=10;
int word_count=0;
size_t word_length=0;
char** pWords=(char**)calloc(maxWords,sizeof(char*));
int* pnWord=(int*)calloc(maxWords,sizeof(int));
char* pWord=strtok(pStr,delimiters);
if(!pWord)
{
printf("No words found.Ending programe.\n");
return 1;
}
bool new_word=true;
while(pWord)
{
for(int i=0;i<word_count;++i)
{
if(strcmp(*(pWords+i),pWord)==0)
{
++*(pnWord+i);
new_word=false;
break;
}
}
}
if(new_word)
{
if(word_count==maxWords)
{
maxWords+=WORDS_INCR;
pWords=(char**)realloc(pWords,maxWords*sizeof(char*));
pnWord=(int*)realloc(pnWord,maxWords*sizeof(int));
}
word_length=strlen(pWord)+1;
(pWords+word_count)=(char)malloc(word_length);
strcpy(*(pWords+word_count),pWord);
*(pnWord+word_count++)=1;
}
else
new_word=true;
pWord=strtok(NULL,delimiters);

for(int i=0;i<word_count;++i)
{
printf("%-13s %3d",*(pWords+i),*(pnWord+i));
if((i+1)%4==0)
printf("\n");
}
printf("\n");
for(int k=0;k<word_count;++k)
{
free(*(pWords+k));
*(pWords+k)=NULL;
}

free(pWords);
pWords=NULL;
free(pnWord);
pnWord=NULL;
free(pStr);
pStr=NULL;
return 0;
}

  • 写回答

2条回答

  • abc_def_ghj 2015-07-08 08:17
    关注

    请看问题1和问题2的修改。由于不知道你的功能,只能根据编译错误进行修改
    char* pStr=(char*)malloc(str_size);
    pStr='\0'; //这里赋空会导致strlen(pStr)为空指针
    printf("Enter some prose with up to %d characters per line.\nTerminate input by entering an empty line:\n",BUF_LEN);
    while(true)
    {
    fgets(buf,BUF_LEN,stdin);
    if(buf[0]=='\n')
    break;
    if(strlen(pStr)+strlen(buf)+1>str_size)
    {
    str_size=strlen(pStr)+strlen(buf)+1;
    pStr=(char*)realloc(pStr,str_size); //问题1
    }
    strcat(pStr,buf);
    }
    size_t maxWords=10;
    int word_count=0;
    size_t word_length=0;
    char** pWords=(char**)calloc(maxWords,sizeof(char*));
    int* pnWord=(int*)calloc(maxWords,sizeof(int));
    char* pWord=strtok(pStr,delimiters);
    if(!pWord)
    {
    printf("No words found.Ending programe.\n");
    return 1;
    }
    bool new_word=true;
    while(pWord)
    {
    for(int i=0;i<word_count;++i)
    {
    if(strcmp(*(pWords+i),pWord)==0)
    {
    ++*(pnWord+i);
    new_word=false;
    break;
    }
    }
    }
    if(new_word)
    {
    if(word_count==maxWords)
    {
    maxWords+=WORDS_INCR;
    pWords=(char**)realloc(pWords,maxWords*sizeof(char*));
    pnWord=(int*)realloc(pnWord,maxWords*sizeof(int));
    }
    word_length=strlen(pWord)+1;

        *(pWords+word_count) = (char*)malloc(word_length); //问题2
        strcpy(*(pWords+word_count),pWord);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮