Compile error at testing Function Pointer

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);
}
}
 
I

Ian Collins

Denny said:
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??

void main(void)

That should be int main(void).
while (stricmp(list[ctr], "QUIT") != NULL)

You don't declare stricmp.
int sort_a_to_z(const void *first, const void *second)
{
return(strcmp((char*)first, (char*)second);

Missing )
}

int sort_z_to_a(const void *first, const void *second)
{
return(strcmp((char*)second, (char*)first);
Missing )
 
H

Harald van Dijk

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 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);
}
}

In addition to what Ian Collins said, you need to move sort_a_to_z and
sort_z_to_a outside main. Standard C doesn't have nested functions, and
even on some compilers that support them as an extension, it wouldn't work
the way you've defined the functions.
 
U

user923005

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]);

In addition to what everyone else said:
http://home.att.net/~jackklein/ctips01.html#safe_gets

[snip]
 
B

Barry Schwarz

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

It would be nice if you told us where the error occurred.
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)

Already covered.
{

int ctr = 0;
int total;
char list[10][256];

printf("\n\nPress <Enter> after each word. Enter QUIT to end\n");

gets(list[ctr]);

You have no idea if 256 characters will be sufficient to hold the
input.
while (stricmp(list[ctr], "QUIT") != NULL)

stricmp is a common but not a standard function. The common ones
return an int. NULL can legally be defined as (void*)0. On systems
which do so this is a constraint violation requiring a diagnostic. If
you want to compare to 0, use 0.
{
ctr++;
if(ctr == 10)
break;

gets(list[ctr]);
}
total = ctr;

qsort((void *)list, total, sizeof(list[0]), sort_a_to_z);

The cast is unnecessary.
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)

Already covered.
{
return(strcmp((char*)first, (char*)second);

Already covered.
}

int sort_z_to_a(const void *first, const void *second)
{
return(strcmp((char*)second, (char*)first);
}
}


Remove del for email
 
P

pete

=?UTF-8?q?Harald_van_D=C4=B3k?= said:
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 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);
}
}

In addition to what Ian Collins said,
you need to move sort_a_to_z and sort_z_to_a outside main.
Standard C doesn't have nested functions,
and even on some compilers that support them as an extension,
it wouldn't work the way you've defined the functions.

A function definition is the only kind of declaration
not allowed inside of a function definition.

A function definition is the only kind of declaration
not terminated by a semicolon.
 
H

Harald van Dijk

A function definition is the only kind of declaration not allowed inside
of a function definition.

A function definition is the only kind of declaration not terminated by
a semicolon.

A function definition is the only kind of declaration that isn't
syntactically a declaration.
 
P

pete

=?UTF-8?q?Harald_van_D=C4=B3k?= wrote:
A function definition is the only kind of declaration that isn't
syntactically a declaration.

I have no idea of what you think you mean by that.
 
B

Ben Pfaff

pete said:
I have no idea of what you think you mean by that.

A function definition is not a declaration, because it does not
end with a semicolon:

declaration:
declaration-specifiers init-declarator-listopt ;
 
P

pete

Ben said:
A function definition is not a declaration, because it does not
end with a semicolon:

declaration:
declaration-specifiers init-declarator-listopt ;

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top