[RESOLVED] void pointer argument in a function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] void pointer argument in a function

  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
    Posts
    12,098

    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
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,396

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,396

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,895

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center