| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 955 人关注过本帖
标题:当NUM_P=100时,为什么这段代码不能实现多个字符串的输入,求大神指导
只看楼主 加入收藏
Bingo_yue
Rank: 2
等 级:论坛游民
帖 子:11
专家分:10
注 册:2016-1-10
结帖率:100%
收藏
 问题点数:0 回复次数:3 
当NUM_P=100时,为什么这段代码不能实现多个字符串的输入,求大神指导
while((*gets(buffer) !='\n') && (i<NUM_P))
{
pS[i]=(char *)malloc(strlen(buffer)+1);

if(pS[i]==NULL)                            /* Check for no memory allocated */
{
      printf("Memory allocation failed.Program terminated.\n");
      return;
}
strcpy(pS[i++],buffer);


}
搜索更多相关主题的帖子: 字符串 
2016-01-11 14:19
Bingo_yue
Rank: 2
等 级:论坛游民
帖 子:11
专家分:10
注 册:2016-1-10
收藏
得分:0 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define  BUFFER_LEN  100                    /* Length of input buffer */
#define  NUM_P  100                         /* maximum number of string */
#define  TRUE  1
#define  FALSE 0

void main()
{
char buffer[BUFFER_LEN];                                /*Store for string */
char *pS[NUM_P]= { NULL };                                   /*Array of string pointers */
char *pTemp = NULL;                                 /* Pointer to buffer */
int i;                                                  /* Loop counter */
int sorted = FALSE;
int last_string = 0;                                                /* store the final i */

/*read the string from the keyboard */

printf("\nEnter successive lines,pressing Enter at the end of each line"
                   ".\n Just press Enter to end.\n\n");

while((*gets(buffer) !='\n') && (i<NUM_P))
{
pS[i]=(char *)malloc(strlen(buffer)+1);

if(pS[i]==NULL)                            /* Check for no memory allocated */
{
      printf("Memory allocation failed.Program terminated.\n");
      return;
}
strcpy(pS[i++],buffer);
}
last_string=i;
}
2016-01-11 14:21
wmf2014
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:216
帖 子:2039
专家分:11273
注 册:2014-12-6
收藏
得分:0 
你没有对变量i初始化就使用,造成内存写入错误。调试时pS[i]=(char *)malloc(strlen(buffer)+1);i=-858993460,所以pS[i]=ps[-858993460]。

能编个毛线衣吗?
2016-01-11 16:16
Bingo_yue
Rank: 2
等 级:论坛游民
帖 子:11
专家分:10
注 册:2016-1-10
收藏
得分:0 
回复 3楼 wmf2014
哦哦  确实忘记初始化了  把i=0;后   程序运行正常了  谢谢啦
2016-01-12 12:48
快速回复:当NUM_P=100时,为什么这段代码不能实现多个字符串的输入,求大神指导
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017464 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved