新手,初学程序结构
程序代码:// 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 **);
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;
int main(void)
{
//
char *pS[NUM_P];
int count = 0;
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++)
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("\nError reading string.\n");
exit(1);
}
if(buffer[0] == '\0')
return false;
*pString = (char*)malloc(strlen(buffer) + 1);
if(*pString == NULL)
{
printf("\nOut of memory.");
exit(1);
}
strcpy(*pString, buffer);
return true;
}
//
//
void str_sort(const char *p[], int n)
{
char *ptemp = NULL;
bool sorted = false;
while(!sorted)
{
sorted = true;
for(int i = 0 ; i<n-1 ; i++)
if(strcmp(p[i], p[i + 1]) > 0)
{
sorted = false;
swap(&p[i], &p[i+1]); /* undefined reference to `swap'|*/
}
}
}
void swap( void **p1, void **p2)
{
void *pt = *p1;
*p1 = *p2;
*p2 = pt;
}
//
void str_out(char *p[] , int n)
{
printf("\nYou input sorted in orde is:\n\n");
for(int i = 0 ; i<n ; i++)
{
printf("%s\n", p[i]);
free(p[i]);
p[i] = NULL;
}
return;
}
用codeblock编译只有一个错误;提示56行的swap(),未定义,在把swap()前置后,编译成功 (但是存储似乎出错了,返回1)这是书上的示例,请问:
1,这是编译器造成的么? 因为用vs2017 报了20个错.
2,在开头不是声明了原型么(是这么说吧?)void swap( void **p1, void **p2);为什么还会这样?
程序代码://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 **);
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;
int main(void)
{
////
////
bool str_in(char **pString)
{
char buffer[BUFFER_LEN];
if(gets(buffer) == NULL)
{
printf("\nError reading string.\n");
exit(1);
}
if(buffer[0] == '\0')
return false;
*pString = (char*)malloc(strlen(buffer)+1);
if(*pString == NULL)
{
printf("\nOut of memory.");
exit(1);
}
strcpy(*pString, buffer);
return true;
}
///////////////
////////////
void swap(void **p1, void **p2) /*这个必须放在下一个函数的前面,但书上是在后面的*/
{
void *pt = *p1;
p1 = *p2;
*p2 = pt;
}
///////////
//////////
void str_sort(const char *p[], int n)
{
char *pTemp = NULL;
bool sorted = false;
while(!sorted)
{
sorted = true;
for(int i = 0; i<n-1; i++)
if(strcmp(p[i], p[i + 1]) > 0)
{
sorted = false;
swap(&p[i], &p[i+1]);
}
}
}
///////////
///////////
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]);
free(p[i]);
p[i] = NULL;
}
return ;
}
char *pS[NUM_P]; /*这个原本在前面 */
int count = 0;
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++)
if(!str_in(&pS[count]))
break;
str_sort( pS, count);
str_out( pS, count);
return 0; /*int main(void)的大括号本来是到这的*/
}昨天太晚,神志不清,今天重新抄了编代码,运行正常,带还是有2个问题:
1,swap()函数必须放在需要调用它的str_sort()前边,书上是放在后边的,而且main()前声明过模型,请问这是怎么回事?
2,int main(void)函数本来是写在前面的,而且大括号是独立的,但是编译之后,自动把所有函数括了进来,还把char*pS 到 return0,放到了最后,
这是怎么了?
使用code blocke17.12
[此贴子已经被作者于2019-4-22 21:54编辑过]



