| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 564 人关注过本帖
标题:C语言入门经典 例题7.14 Visual studio 2013 无法运行
只看楼主 加入收藏
eruera
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2014-12-10
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
C语言入门经典 例题7.14 Visual studio 2013 无法运行
C语言入门经典 例题7.14
/* Program 7.14 Sorting strings */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define BUFFER_LEN 100 /* Length of input buffer */
#define NUM_P 100 /* maximum number of strings */


int main(void)
{
char buffer[BUFFER_LEN]; /* space to store an input string */
char *pS[NUM_P] = { NULL }; /* Array of string pointers */
char *pTemp = NULL; /* Temporary pointer */
int i = 0; /* Loop counter */
bool sorted = false; /* Indicated when strings are sorted */
int last_string = 0; /* Index of last string entered */


printf("\nEnter successive lines, pressing Enter at the"
" end of each line.\nJust press Enter to end.\n\n");
while((*fgets(buffer, BUFFER_LEN, stdin) != '\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 1;
}
strcpy(pS[i++], buffer);
}
last_string = i; /* Save last string index */


/* Sort the strings in ascending order */
while(!sorted)
{
sorted = true;
for(i = 0 ; i<last_string-1 ; i++)
if(strcmp(pS[i], pS[i + 1]) > 0)
{
sorted = false; /* We were out of order */
pTemp= pS[i]; /* Swap pointers pS[i] */
pS[i] = pS[i + 1]; /* and */
pS[i + 1] = pTemp; /* pS[i + 1] */
}
}


/* Displayed the sorted strings */
printf("\nYour input sorted in order is:\n\n");
for(i = 0 ; i<last_string ; i++)
{
printf("%s\n", pS[i] );
free( pS[i] );
pS[i] = NULL;
}
return 0;
}
代码如上,编译后按照错误提示修改函数strcpy(pS[i++], buffer)为strcpy_s(pS[i++],BUFFER_LEN, buffer);,编译通过,然后运行,只要输入超过两行句子程序就崩溃了,这是什么原因啊!
搜索更多相关主题的帖子: counter include number C语言 false 
2014-12-10 20:48
longwu9t
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:732
专家分:2468
注 册:2014-10-9
收藏
得分:10 
图片附件: 游客没有浏览图片的权限,请 登录注册

vs2013中正常生成 运行正常

是不是你的程序源文件的后缀名有问题

用.c不要用.cpp

Only the Code Tells the Truth             K.I.S.S
2014-12-11 16:51
xiaoguotong
Rank: 2
等 级:论坛游民
帖 子:2
专家分:10
注 册:2013-1-30
收藏
得分:10 
回复 楼主 eruera
while(!sorted)
{
sorted = true;
for(i = 0 ; i<last_string-1 ; i++)
if(strcmp(pS[i], pS[i + 1]) > 0)
{
sorted = false; /* We were out of order */
pTemp= pS[i]; /* Swap pointers pS[i] */
pS[i] = pS[i + 1]; /* and */
pS[i + 1] = pTemp; /* pS[i + 1] */
}
}

for循环加()试试
2014-12-12 11:14
eruera
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2014-12-10
收藏
得分:0 
回复 2楼 longwu9t
在代码第一行加#define _CRT_SECURE_NO_WARNINGS忽视代码安全问题,代码中仍然使用strcpy就可以了
2014-12-13 21:41
eruera
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2014-12-10
收藏
得分:0 
回复 3楼 xiaoguotong
在代码第一行加#define _CRT_SECURE_NO_WARNINGS忽视这个问题,代码中仍然使用strcpy就可以了
2014-12-13 21:51
快速回复:C语言入门经典 例题7.14 Visual studio 2013 无法运行
数据加载中...
 
   



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

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