Results 1 to 8 of 8

Thread: [RESOLVED] Some kind of data type issue while trying to pass parameters to a procedure->

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Resolved [RESOLVED] Some kind of data type issue while trying to pass parameters to a procedure->

    Code:
    int main(int argc, char* argv[]) {
    	processCompressedFiles(argv[1], argv[2]);
    	return 0;
    }
    
    void processCompressedFiles(char* sourcePath, char* targetPath) {
    
    }
    produces this error:

    Code:
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	C2371	'processCompressedFiles': redefinition; different basic types	Stunts Unpacker	D:\Other\stunpack.rewrite\Stunts Unpacker\Core.c	5
    It would seem simple enough to pass two strings as parameters to a procedure which accepts two strings... But, it doesn't work. Does anyone who has a better grasp on C than me know what I am missing? Probably something very simple for a more experienced C developer.

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Some kind of data type issue while trying to pass parameters to a procedure->

    Is this simply because you haven't declared a function prototype above main?

    Code:
    void processCompressedFiles(char *, char *);
    I tested your code out in Borland C++ 2.0 for DOS (Copyright 1991, the version I used back in high school, I keep a copy of it accessible for nostalgia), and it worked only after I added the function prototype to the top.

  3. #3
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Some kind of data type issue while trying to pass parameters to a procedure->

    I would also recommend wrapping that call to processCompressedFiles in a:

    Code:
    if (argc >= 3)
    statement, since you will likely get unexpected results running that code and passing 0 or 1 "extra" command line parameters. Note that (and you probably already know this, but I thought I would mention it), by default, with no specified extra parameters, argc will = 1, and argv[0] will always* contain the full path to the executable being ran.

    *That has been the case for the various C/C++ compilers I've used on occasion over the years, most of which are now very old. Not a guarantee that that is still always the case with current C++ compilers.
    Last edited by OptionBase1; Mar 15th, 2023 at 03:43 PM.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Some kind of data type issue while trying to pass parameters to a procedure->

    @OptionsBase1: thank you! you're life saver. I didn't know those prototypes were required to prevent this issue. I thought they were boilerplate.

    (The most recent (and thus very obsolete) language I used that required that sort of thing was Q(uick) Basic. Which generated those prototypes itself instead of throwing cryptic errors at you if you forgot them. I am so used to Basic I forget that C is more low level and therefore requires more effor from the developer.)\

    BTW:
    Funnily enough if I convert my void procedure into a function of type "int" and have it return a value I don't need to define a prototype. Why is that?

  5. #5
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Some kind of data type issue while trying to pass parameters to a procedure->

    Quote Originally Posted by Peter Swinkels View Post
    BTW:
    Funnily enough if I convert my void procedure into a function of type "int" and have it return a value I don't need to define a prototype. Why is that?
    Hmm...I tested and verified that I can reproduce this in BC++ 2.0 as well. Honestly, I don't know why a "void" method wouldn't work without a prototype, but a function with a return type would.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Some kind of data type issue while trying to pass parameters to a procedure->

    Weird, but not really important at the moment. That however, might change when I get stuck with my project again. I am marking this thread as resolved. :-)

  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Some kind of data type issue while trying to pass parameters to a procedure->

    Ok, found this that explains it:

    https://stackoverflow.com/questions/...-function-in-c

    If you don't have a forward declaration of your function before the place of usage, the compiler will create implicit declaration for you - with the signature int input(). It will take the name of the function you called, it will assume that the function is returning int, and it can accept any arguments.
    So, because the compiler tries to be helpful, it will work without a prototype only for functions that return an int, which you chose by happenstance I'm assuming. Try changing the return type to long or float and you should see the same error you were getting earlier.

    All that being said, you don't need to define any prototypes so long as the code for any functions that are called by any other functions exist "above the caller" in code. So, if you were to simply move your "processCompressedFiles" method in its entirety above your "main" method, then no prototype is needed, since the compiler already has "seen" (going from the top down) the method when it is called in main.
    Last edited by OptionBase1; Mar 15th, 2023 at 04:04 PM.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: [RESOLVED] Some kind of data type issue while trying to pass parameters to a proc

    C being helpful? That's a new one for me. I guess it doesn't completely throw you into the deep...

    Oh, and I forgot to reply to your "if (argc >= 3)" comment:
    I already have that, in fact I have a lot more code than I posted. I just posted what I thought that which was relevant to my question. I hate it when people throw an entire module of code at you for one issue. Usually it's because they are new and don't seem to know how to post proper questions... But that's off topic... Again, thank you!

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