CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2014
    Posts
    2

    [RESOLVED] void pointer argument in a function

    Hi all,

    Why does the following code compile and execute without any error? I mean, the function compareid should get 2 arguments so why does the compiler not complaining, is it because of the type of arguments?
    Thanks.


    Code:
    #include <stdio.h>
    int compareid(void* info, int value);  // ansi declaration
    
    int compareid(void* info, int value)
       /* Some code here */
       return( foundItem == value );
    }
    
    int main( void ){ 
       /* more code...*/
    
       compareid;
       printf("\nThis program works\n");
    
       return(0);
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: void pointer argument in a function

    Doesn't compile for me.

    1>c:\cg\cg\cg.cpp(7): error C2143: syntax error : missing ';' before 'return'
    1>c:\cg\cg\cg.cpp(8): error C2059: syntax error : '}'
    1>c:\cg\cg\cg.cpp(8): error C2143: syntax error : missing ';' before '}'
    1>c:\cg\cg\cg.cpp(10): error C2143: syntax error : missing ';' before '{'
    1>c:\cg\cg\cg.cpp(10): error C2447: '{' : missing function header (old-style formal list?)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: void pointer argument in a function

    Quote Originally Posted by StephaneLeFou View Post
    Hi all,

    Why does the following code compile and execute without any error? I mean, the function compareid should get 2 arguments so why does the compiler not complaining, is it because of the type of arguments?
    Quote Originally Posted by GCDEF View Post
    Doesn't compile for me.

    1>c:\cg\cg\cg.cpp(7): error C2143: syntax error : missing ';' before 'return'
    1>c:\cg\cg\cg.cpp(8): error C2059: syntax error : '}'
    1>c:\cg\cg\cg.cpp(8): error C2143: syntax error : missing ';' before '}'
    1>c:\cg\cg\cg.cpp(10): error C2143: syntax error : missing ';' before '{'
    1>c:\cg\cg\cg.cpp(10): error C2447: '{' : missing function header (old-style formal list?)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    I had the same result as GCDEF.

    Dear StephaneLeFou,
    please, next time check if your pseudo/code does compile before posting it to Forum!
    Victor Nijegorodov

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: void pointer argument in a function

    Are you compiling as c or as c++?

    Where is foundItem defined?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: void pointer argument in a function

    If compiled as c as opposed to c++, the program below compiles and executes.
    Code:
    #include <stdio.h>
    int compareid(void* info, int value);  // ansi declaration
    
    int compareid(void* info, int value)
    {
    int foundItem = 1;
       /* Some code here */
       return( foundItem == value );
    }
    
    int main( void ){ 
       /* more code...*/
    
       compareid;
       printf("\nThis program works\n");
    
       return(0);
    }
    There is a warning 'expression evaluates to a function which is missing an argument list'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: [RESOLVED] void pointer argument in a function

    Quote Originally Posted by StephaneLeFou View Post
    Code:
       compareid;
    }
    This is not a function call. it is just an identifier, which ends up doing nothing in the compiled code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured