Compile error at testing Function Pointer

Discussion in 'C Programming' started by Denny, Feb 27, 2008.

  1. Denny

    Denny Guest

    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);
    }
    }
    Denny, Feb 27, 2008
    #1
    1. Advertising

  2. Denny

    Ian Collins Guest

    Denny wrote:
    > 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 )

    --
    Ian Collins.
    Ian Collins, Feb 27, 2008
    #2
    1. Advertising

  3. On Tue, 26 Feb 2008 22:09:23 -0800, Denny wrote:
    > 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.
    Harald van Dijk, Feb 27, 2008
    #3
  4. Denny

    user923005 Guest

    On Feb 26, 10:09 pm, Denny <> wrote:
    > 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]
    user923005, Feb 27, 2008
    #4
  5. On Tue, 26 Feb 2008 22:09:23 -0800 (PST), Denny <>
    wrote:

    >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
    Barry Schwarz, Feb 28, 2008
    #5
  6. Denny

    pete Guest

    =?UTF-8?q?Harald_van_D=C4=B3k?= wrote:
    >
    > On Tue, 26 Feb 2008 22:09:23 -0800, Denny wrote:
    > > 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.

    --
    pete
    pete, Feb 28, 2008
    #6
  7. On Thu, 28 Feb 2008 06:35:22 -0500, pete wrote:
    > 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.
    Harald van Dijk, Feb 28, 2008
    #7
  8. Denny

    pete Guest

    =?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.

    --
    pete
    pete, Feb 28, 2008
    #8
  9. Denny

    Ben Pfaff Guest

    pete <> writes:

    > =?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.


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

    declaration:
    declaration-specifiers init-declarator-listopt ;
    --
    "I hope, some day, to learn to read.
    It seems to be even harder than writing."
    --Richard Heathfield
    Ben Pfaff, Feb 28, 2008
    #9
  10. Denny

    pete Guest

    Ben Pfaff wrote:
    >
    > pete <> writes:
    >
    > > =?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.

    >
    > A function definition is not a declaration, because it does not
    > end with a semicolon:
    >
    > declaration:
    > declaration-specifiers init-declarator-listopt ;


    Thank you.

    --
    pete
    pete, Feb 28, 2008
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. glen stark
    Replies:
    2
    Views:
    689
    Ron Natalie
    Oct 10, 2003
  2. Fraser Ross
    Replies:
    4
    Views:
    1,026
    Fraser Ross
    Aug 14, 2004
  3. murgan
    Replies:
    6
    Views:
    4,840
    Thad Smith
    Dec 21, 2005
  4. Nagaraj
    Replies:
    1
    Views:
    840
    Lionel B
    Mar 1, 2007
  5. Alex Buell
    Replies:
    14
    Views:
    796
    Thomas J. Gritzan
    Dec 28, 2008
Loading...

Share This Page