C程序运行崩溃,该如何处理
C程序运行崩溃
程序弄了好久终于编译通过了,但是功能受损,运行崩溃。大家多多指点!
编译环境是win7上的 vc 6.0,其实我一直都在虚拟机上的XP编译,以为是虚拟机的问题又回到win7运行了一次,还是崩溃。
还有main函数之前用了宏定义,可之前我是用const的语句,因为代码是按某本经典入门书上敲的,没想到不能运行,觉得好坑!
按错误提示,const的BUFFER_LEN 在定义数组char[BUFFERX _LEN]的大小时是无效的
这做何解释?
/* Program 8.6 使用指针传输数据*/
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
bool str_in(char **);
void str_sort(const char *[],int);
void swap(void **p1,void **p2); /*交换两个指针*/
void str_out(char *[],int);
//const size_t BUFFER_LEN=256;
//const size_t NUM_P=50;
#define BUFFER_LEN 256
#define NUM_P 50
int main(void)
{
char *pS[NUM_P];
int count=0;
printf("连续输入几行,在每行结束处输入ENTER.\n");
for(count=0;count<NUM_P;count++)
if(!str_in(&pS[count]))
break;
str_sort(pS,count);
str_out(pS,count);
return 0;
}
bool str_in(char **pString)
{
char buffer[BUFFER_LEN];
if(gets(buffer)==NULL)
{
printf("\n字符串读取错误。\n");
exit(1);
}
if(buffer[0]=='\0') //空字符串读取
return false;
*pString=(char*)malloc(strlen(buffer)+1);
if(*pString==NULL) //检查内存
{
printf("\n内存不足");
exit(1);
}
strcpy(*pString,buffer);
return true;
}
void str_sort(const char *p[],int n)
{
char *pTemp=NULL;
bool sorted=false;
while(!sorted)
{
int i;
sorted=true;
for(i=0;i<n-1;i++)
if(strcmp(p[i],p[i+1])>0)
{
sorted=false;
swap(&p[i],&p[i+1]);
}
}
}
void swap(void **p1,void **p2)
{
void *pt=p1;
*p1=*p2;
*p2=pt;
}
void str_out(char *p[],int n)
{
int i;
printf("\n你的输入排序之后为:\n\n");
for(i=0;i<n;i++)
{
printf("%s\n",p[i]);
free(p[i]);
p[i]=NULL;
}
return;
}
------解决方案--------------------
点击重试看看什么地方出错了
------解决方案--------------------
bool str_in(char **);
???????????????
bool str_in(char *);
在CSDN呆了好几天,没意思。
------解决方案--------------------
void *pt=p1
不要说咱眼力好,一看断言,我就知道那错了
程序弄了好久终于编译通过了,但是功能受损,运行崩溃。大家多多指点!
编译环境是win7上的 vc 6.0,其实我一直都在虚拟机上的XP编译,以为是虚拟机的问题又回到win7运行了一次,还是崩溃。
还有main函数之前用了宏定义,可之前我是用const的语句,因为代码是按某本经典入门书上敲的,没想到不能运行,觉得好坑!
按错误提示,const的BUFFER_LEN 在定义数组char[BUFFERX _LEN]的大小时是无效的
这做何解释?
/* Program 8.6 使用指针传输数据*/
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
bool str_in(char **);
void str_sort(const char *[],int);
void swap(void **p1,void **p2); /*交换两个指针*/
void str_out(char *[],int);
//const size_t BUFFER_LEN=256;
//const size_t NUM_P=50;
#define BUFFER_LEN 256
#define NUM_P 50
int main(void)
{
char *pS[NUM_P];
int count=0;
printf("连续输入几行,在每行结束处输入ENTER.\n");
for(count=0;count<NUM_P;count++)
if(!str_in(&pS[count]))
break;
str_sort(pS,count);
str_out(pS,count);
return 0;
}
bool str_in(char **pString)
{
char buffer[BUFFER_LEN];
if(gets(buffer)==NULL)
{
printf("\n字符串读取错误。\n");
exit(1);
}
if(buffer[0]=='\0') //空字符串读取
return false;
*pString=(char*)malloc(strlen(buffer)+1);
if(*pString==NULL) //检查内存
{
printf("\n内存不足");
exit(1);
}
strcpy(*pString,buffer);
return true;
}
void str_sort(const char *p[],int n)
{
char *pTemp=NULL;
bool sorted=false;
while(!sorted)
{
int i;
sorted=true;
for(i=0;i<n-1;i++)
if(strcmp(p[i],p[i+1])>0)
{
sorted=false;
swap(&p[i],&p[i+1]);
}
}
}
void swap(void **p1,void **p2)
{
void *pt=p1;
*p1=*p2;
*p2=pt;
}
void str_out(char *p[],int n)
{
int i;
printf("\n你的输入排序之后为:\n\n");
for(i=0;i<n;i++)
{
printf("%s\n",p[i]);
free(p[i]);
p[i]=NULL;
}
return;
}
------解决方案--------------------
点击重试看看什么地方出错了
------解决方案--------------------
bool str_in(char **);
???????????????
bool str_in(char *);
在CSDN呆了好几天,没意思。
------解决方案--------------------
void *pt=p1
不要说咱眼力好,一看断言,我就知道那错了