D
Denny
When I compiled this C source, a C compiler spat out a message which
was "Declaration syntax error
in function main()". I am having tested function pointer example
program. Of course, I am a novice at C so
that I don't know exactly which part is incorrect. Would you help me??
-----------------------------sort.c-------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int sort_a_to_z(const void *first, const void *second);
int sort_z_to_a(const void *first, const void *second);
void main(void)
{
int ctr = 0;
int total;
char list[10][256];
printf("\n\nPress <Enter> after each word. Enter QUIT to end\n");
gets(list[ctr]);
while (stricmp(list[ctr], "QUIT") != NULL)
{
ctr++;
if(ctr == 10)
break;
gets(list[ctr]);
}
total = ctr;
qsort((void *)list, total, sizeof(list[0]), sort_a_to_z);
printf("\nThe items sorted A to Z\n");
for(ctr = 0; ctr < total; ctr++)
{
printf("\n%s", list[ctr]);
}
qsort((void *)list, total, sizeof(list[0]), sort_z_to_a);
printf("\n\nThe items sorted Z to A\n");
for(ctr = 0; ctr < total; ctr++)
{
printf("\n%s", list[ctr]);
}
int sort_a_to_z(const void *first, const void *second)
{
return(strcmp((char*)first, (char*)second);
}
int sort_z_to_a(const void *first, const void *second)
{
return(strcmp((char*)second, (char*)first);
}
}
was "Declaration syntax error
in function main()". I am having tested function pointer example
program. Of course, I am a novice at C so
that I don't know exactly which part is incorrect. Would you help me??
-----------------------------sort.c-------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int sort_a_to_z(const void *first, const void *second);
int sort_z_to_a(const void *first, const void *second);
void main(void)
{
int ctr = 0;
int total;
char list[10][256];
printf("\n\nPress <Enter> after each word. Enter QUIT to end\n");
gets(list[ctr]);
while (stricmp(list[ctr], "QUIT") != NULL)
{
ctr++;
if(ctr == 10)
break;
gets(list[ctr]);
}
total = ctr;
qsort((void *)list, total, sizeof(list[0]), sort_a_to_z);
printf("\nThe items sorted A to Z\n");
for(ctr = 0; ctr < total; ctr++)
{
printf("\n%s", list[ctr]);
}
qsort((void *)list, total, sizeof(list[0]), sort_z_to_a);
printf("\n\nThe items sorted Z to A\n");
for(ctr = 0; ctr < total; ctr++)
{
printf("\n%s", list[ctr]);
}
int sort_a_to_z(const void *first, const void *second)
{
return(strcmp((char*)first, (char*)second);
}
int sort_z_to_a(const void *first, const void *second)
{
return(strcmp((char*)second, (char*)first);
}
}