// p8.4 The functional approach to string sorting
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define BUF_LEN 256 // Input buffer length
#define INIT_NSTR 2 // Initial number of strings
#define NSTR_INCR 2 // Increment to number of strings
char* str_in(); // Reads a string
void str_sort(const char**, size_t); // Sorts an array for strings
void swap(const char**, const char**); // Swaps two pointers
void str_out(const char* const*, size_t); // Output the strings
void free_memory(char**, size_t); // Free all heap memory
// Function main - execution starts here
int main(void)
{
size_t pS_size = INIT_NSTR; // count of pS elements
char **pS = calloc(pS_size, sizeof(char*)); // Array of string pinters
if(!pS)
{
printf("Failed to allocate memory for string pointers.\n");
exit(1);
}
char **pTemp = NULL; // Tmeporary pointer
size_t str_count = 0; // Number of strings read
char *pStr = NULL; // String pointer
printf("Enter one string per line. Press Enter to end:\n");
while((pStr = str_in()) != NULL)
{
if(str_count == pS_size)
{
pS_size += NSTR_INCR;
if(!(pTemp = realloc(pS, pS_size*sizeof(char*))))
{
printf("Memory allocation for array of strings failed.\n");
return 2;
}
pS = pTemp;
}
pS[str_count++] = pStr;
}
str_sort(pS, str_count); // Sort strings
str_out(pS, str_count); // Output strings
free_memory(pS, str_count); // Free all heap memory
return 0;
}
/*这是在Pelles C编译器中敲出的代码,链接时显示:
POLINK: error: Unresolved external symbol 'str_in'.
POLINK: error: Unresolved external symbol 'str_sort'.
POLINK: error: Unresolved external symbol 'str_out'.
POLINK: error: Unresolved external symbol 'free_memory'.
编译时显示:
F:\c_files\p8.4.c(48): warning #2145: Assignment of 'char * *' to 'const char * *'.
F:\c_files\p8.4.c(49): warning #2145: Assignment of 'char * *' to 'const char * const *'.
不知道问题在哪*/
目前暂无任何回答
- 0 回答
- 0 关注
- 1229 浏览
添加回答
举报
0/150
提交
取消