Debug_fault,该如何处理
Debug_fault
前辈们:请看下边的程序,在编译时出错,不知如何是好,请多多指教。
·
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
bool str_in(char **); //function prototype
void str_sort(const char *[],int); //function prototype
void swap(void **p1,void **p2); //swap two pointers
void str_out(char *[],int);//function prototype for str_out
const size_t BUFFER_LEN=256;
const size_t NUM_P=50;
int main(void)
{
char *pS[NUM_P]; //Array of string pointers
int count=0; //number of string read
printf("\nEnter successive lines,pressing Enter at the end of"
"each line.\nJust press Enter to end.\n");
for(count=0;count<NUM_P;count++)//max of NUM_P strings
if(!str_in(&pS[count])) //read a string
break; //stop input on 0 return
str_sort(pS,count); //sort string
str_out(pS,count); //output string
return 0;
}
/**********************************************
String input routine //
Argument is a pointer to a pointer to a constant
string which is const char** *
Returns fales for empty string and Returns true
otherwise. if no memory is abtained or if there
is an error reading from the keyboard,the program
is terminated by calling exit
************************************************/
bool str_in(char **pString)
{
char buffer[BUFFER_LEN];//space to store input string
if(gets(buffer)==NULL) //null returned from gets()?
{
printf("\nError reading string.\n");
exit(1); //Error on input so exit
}
if(buffer[0]=='\0')//empty string read?
return false;
*pString=(char*)malloc(strlen(buffer)+1);
if(*pString==null) //check memory allocation
{
printf("\nOut of memory");
exit(1); //no memory allocated so exit
}
strcpy(*pString,buffer);//copy string read to argument
return true;
}
/***********************************************
String sort routine
First argument is array of pointers to constant
strings which is of type const char*[].
Second argument is the number of elements in the
pointer array i.e.the number of strings
***********************************************/
void str_sort(const char *p[],int n)
{
char *pTemp=NULL;//temporary pointer
bool sorted=false;//string sorted indicator
while(!sorted) //loop until there are no swaps
{
sorted=1; //initialize to indicate no swaps
for(int i=0;i<n-1;i++)
if(strcmp(p[i],p[i+1])>0)
{
sorted=0;//indicate we are out of order
swap(&p[i],&p[i+1]);//swap the pointer
}
}
}
/******************************************************
swap two pointer
The arguments are type pointer to void
so pointers can be any type
*****************************************************/
void swap(void **p1,void **p2)
{
void *pt=*p1;
*p1=*p2;
*p2=pt;
}
/******************************************************
string output routine
Fisrt argument is an array of pointers to strings
witch is the same as char** *
The second argument is a count of the number of
pointers in the array i.e.the number of strings
*****************************************************/
void str_out(char *p[],int n)
{
printf("\nYour input sorted in order is:\n\n");
for(int i=0;i<n;i++)
{
printf("%s\n",p[i]);//display a strings
free(p[i]); //free memory for the strings
p[i]=NULL;
}
}
------解决思路----------------------
参数的类型是指向 const char * 的指针;ps的类型是指向char *的指针。形参和实参所指向的类型不兼容。
参见以下这个帖子的46楼:http://bbs.****.net/topics/390946225
------解决思路----------------------

挨个检查修改吧
编译提示的错误比较清楚了
前辈们:请看下边的程序,在编译时出错,不知如何是好,请多多指教。
·
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
bool str_in(char **); //function prototype
void str_sort(const char *[],int); //function prototype
void swap(void **p1,void **p2); //swap two pointers
void str_out(char *[],int);//function prototype for str_out
const size_t BUFFER_LEN=256;
const size_t NUM_P=50;
int main(void)
{
char *pS[NUM_P]; //Array of string pointers
int count=0; //number of string read
printf("\nEnter successive lines,pressing Enter at the end of"
"each line.\nJust press Enter to end.\n");
for(count=0;count<NUM_P;count++)//max of NUM_P strings
if(!str_in(&pS[count])) //read a string
break; //stop input on 0 return
str_sort(pS,count); //sort string
str_out(pS,count); //output string
return 0;
}
/**********************************************
String input routine //
Argument is a pointer to a pointer to a constant
string which is const char** *
Returns fales for empty string and Returns true
otherwise. if no memory is abtained or if there
is an error reading from the keyboard,the program
is terminated by calling exit
************************************************/
bool str_in(char **pString)
{
char buffer[BUFFER_LEN];//space to store input string
if(gets(buffer)==NULL) //null returned from gets()?
{
printf("\nError reading string.\n");
exit(1); //Error on input so exit
}
if(buffer[0]=='\0')//empty string read?
return false;
*pString=(char*)malloc(strlen(buffer)+1);
if(*pString==null) //check memory allocation
{
printf("\nOut of memory");
exit(1); //no memory allocated so exit
}
strcpy(*pString,buffer);//copy string read to argument
return true;
}
/***********************************************
String sort routine
First argument is array of pointers to constant
strings which is of type const char*[].
Second argument is the number of elements in the
pointer array i.e.the number of strings
***********************************************/
void str_sort(const char *p[],int n)
{
char *pTemp=NULL;//temporary pointer
bool sorted=false;//string sorted indicator
while(!sorted) //loop until there are no swaps
{
sorted=1; //initialize to indicate no swaps
for(int i=0;i<n-1;i++)
if(strcmp(p[i],p[i+1])>0)
{
sorted=0;//indicate we are out of order
swap(&p[i],&p[i+1]);//swap the pointer
}
}
}
/******************************************************
swap two pointer
The arguments are type pointer to void
so pointers can be any type
*****************************************************/
void swap(void **p1,void **p2)
{
void *pt=*p1;
*p1=*p2;
*p2=pt;
}
/******************************************************
string output routine
Fisrt argument is an array of pointers to strings
witch is the same as char** *
The second argument is a count of the number of
pointers in the array i.e.the number of strings
*****************************************************/
void str_out(char *p[],int n)
{
printf("\nYour input sorted in order is:\n\n");
for(int i=0;i<n;i++)
{
printf("%s\n",p[i]);//display a strings
free(p[i]); //free memory for the strings
p[i]=NULL;
}
}
------解决思路----------------------
void str_sort (const char * [], int); //function prototype
char * pS [NUM_P]; //Array of string pointers
参数的类型是指向 const char * 的指针;ps的类型是指向char *的指针。形参和实参所指向的类型不兼容。
参见以下这个帖子的46楼:http://bbs.****.net/topics/390946225
------解决思路----------------------
挨个检查修改吧
编译提示的错误比较清楚了