首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

程序出错,帮忙找找原因,该怎么解决

2012-02-15 
程序出错,帮忙找找原因我的编译器是MinGW 4.6.0源代码如下:C/C++ code/* Program 8.6 The functional appr

程序出错,帮忙找找原因
我的编译器是MinGW 4.6.0
源代码如下:

C/C++ code
/* Program 8.6 The functional approach to string sorting */#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#include <string.h>bool str_in(char** pString);              /* Function prototype for str_in */void str_sort(const char* [] , int);      /* Function prototype for str_sort */void swap(void** p1 , void** p2);         /* Swap two pointers */void str_out(char* [] , int);             /* Function prototpye for str_out */const size_t BUFFER_LEN = 256;const size_t NUM_P = 50;/* Function main - execution starts here */int main(void){    char* pS[NUM_P];                      /* Array of string pointers */    int count = 0;                        /* Number of strings 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 strings */    str_out(pS , count);                  /* Output strings */    return 0;}/******************************************************* String input routine                                ** Argument is a pointer to a pointer to a constant    ** string which is const char**                        ** Returns false for empty string and returns true     ** otherwise. If no memory is obtained 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;                  /* Strings sorted indicator */    while(!sorted)                        /* Loop until there are no swaps */    {        sorted = true;                    /* Initialize to indicate no swaps */        for(int i  = 0 ; i < n - 1 ; i++)        {            if(strcmp(p[i] , p[i + 1]) > 0)            {                sorted = false;           /* Indicate we are out of order */                swap(&p[i] , &p[i + 1]);  /* Swap the pointers */            }        }    }}/******************************************* Swap two pointers                       ** 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                             ** First argument is an array of pointers to strings ** which 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 string */        free(p[i]);                        /* Free memory for the string */        p[i] = NULL;    }    return;} 



编译后提示的错误如下:
prog8.6.c: In function 'main':
prog8.6.c:32:2: warning: passing argument 1 of 'str_sort' from incompatible poin
ter type [enabled by default]
prog8.6.c:8:6: note: expected 'const char **' but argument is of type 'char **'
prog8.6.c: In function 'str_sort':
prog8.6.c:92:5: warning: passing argument 1 of 'swap' from incompatible pointer
type [enabled by default]
prog8.6.c:9:6: note: expected 'void **' but argument is of type 'const char **'
prog8.6.c:92:5: warning: passing argument 2 of 'swap' from incompatible pointer
type [enabled by default]
prog8.6.c:9:6: note: expected 'void **' but argument is of type 'const char **'
prog8.6.c: In function 'swap':
prog8.6.c:107:8: warning: dereferencing 'void *' pointer [enabled by default]
prog8.6.c:107:6: error: void value not ignored as it ought to be
请人帮忙看看

[解决办法]
帮你改了过来。
C/C++ code
#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#include <string.h>bool str_in(char** pString);              /* Function prototype for str_in */void str_sort(char* [] , int);      /* Function prototype for str_sort */void swap(char** p1 , char** p2);         /* Swap two pointers */void str_out(char* [] , int);             /* Function prototpye 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 strings 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 strings */    str_out(pS , count);                  /* Output strings */    return 0;}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;}void str_sort( char* p[] , int n){    char* pTemp = NULL;                   /* Temporary pointer */    bool sorted = false;                  /* Strings sorted indicator */    while(!sorted)                        /* Loop until there are no swaps */    {        sorted = true;                    /* Initialize to indicate no swaps */        for(int i  = 0 ; i < n - 1 ; i++)        {            if(strcmp(p[i] , p[i + 1]) > 0)            {                sorted = false;           /* Indicate we are out of order */                swap(&p[i] , &p[i + 1]);  /* Swap the pointers */            }        }    }}void swap(char** p1 , char** p2){    char* pt = *p1;    *p1 = *p2;    *p2 = pt;}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 string */        free(p[i]);                        /* Free memory for the string */        p[i] = NULL;    }    return;}
------解决方案--------------------


探讨

引用:

我的编译器是g++的。改了一些const char*和char* ,然后就是*p2 = *pt;这句,改成*p2 = pt,
还有就是把void*改成了char*.

我按照你给的修改了程序,成功编译并可以运行程序了,不过在输入了第一个字符串后程序就会自动停止(崩溃了),不知道是怎么回事

热点排行