程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> c語言-哪位大神幫我看看代碼有什麼問題

c語言-哪位大神幫我看看代碼有什麼問題

編輯:編程綜合問答
哪位大神幫我看看代碼有什麼問題

程序運行後,輸入字符串沒反應,小弟剛學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;
}

最佳回答:


請看問題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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved