Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C function call question.

C function call question.

Scheduled Pinned Locked Moved C / C++ / MFC
question
14 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    samzcs
    wrote on last edited by
    #1

    I read a program, a function definition like:

    void A( int channel)
    {
    }

    While the caller like:

    A(); //no parameter here.

    I know it's OK. can't find a explanation.

    C Kornfeld Eliyahu PeterK 2 Replies Last reply
    0
    • S samzcs

      I read a program, a function definition like:

      void A( int channel)
      {
      }

      While the caller like:

      A(); //no parameter here.

      I know it's OK. can't find a explanation.

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      why do you say it's OK ? it's not.

      void A( int channel)
      {
      }

      int _tmain(int argc, _TCHAR* argv[])
      {
      A();
      return 0;
      }

      1>c:\temp\fargs.cpp(11) : error C2660: 'A' : function does not take 0 arguments

      image processing toolkits | batch image processing

      S 1 Reply Last reply
      0
      • C Chris Losinger

        why do you say it's OK ? it's not.

        void A( int channel)
        {
        }

        int _tmain(int argc, _TCHAR* argv[])
        {
        A();
        return 0;
        }

        1>c:\temp\fargs.cpp(11) : error C2660: 'A' : function does not take 0 arguments

        image processing toolkits | batch image processing

        S Offline
        S Offline
        samzcs
        wrote on last edited by
        #3

        But in the program, our system. It works. I tested it. just the caller.h: extern void A(void); I debugged the code, it stepped into the A(i); in fact, only one A() function in the program.

        C L K 3 Replies Last reply
        0
        • S samzcs

          But in the program, our system. It works. I tested it. just the caller.h: extern void A(void); I debugged the code, it stepped into the A(i); in fact, only one A() function in the program.

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #4

          what compiler are you using? at best, i would think the behavior is undefined. the function is going to try to find a parameter, somewhere. maybe it pulls some random garbage off the stack where it expects a parameter to be, or maybe your compiler puts variables into certain registers (and so the function will just grab random garbage out of a register).

          image processing toolkits | batch image processing

          S 1 Reply Last reply
          0
          • S samzcs

            But in the program, our system. It works. I tested it. just the caller.h: extern void A(void); I debugged the code, it stepped into the A(i); in fact, only one A() function in the program.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            It looks like you have two different versions of A. But then you are not showing us the complete code.

            1 Reply Last reply
            0
            • C Chris Losinger

              what compiler are you using? at best, i would think the behavior is undefined. the function is going to try to find a parameter, somewhere. maybe it pulls some random garbage off the stack where it expects a parameter to be, or maybe your compiler puts variables into certain registers (and so the function will just grab random garbage out of a register).

              image processing toolkits | batch image processing

              S Offline
              S Offline
              samzcs
              wrote on last edited by
              #6

              It's a GHS compiler. An embedded project.

              _ 1 Reply Last reply
              0
              • S samzcs

                But in the program, our system. It works. I tested it. just the caller.h: extern void A(void); I debugged the code, it stepped into the A(i); in fact, only one A() function in the program.

                K Offline
                K Offline
                k5054
                wrote on last edited by
                #7

                You shoud correct your caller.h to have the correct prototype for A(). Then #include caller.h in both where you define A() and where you invoke it. This "works", due to C calling conventions, where the caller is responsible for pushing and popping arguments off the stack. In your case the caller doesn't push anything on the stack. When A() executes it will use whatever values are on the stack as its parameters, which in this case could be any value at all.

                K 1 Reply Last reply
                0
                • K k5054

                  You shoud correct your caller.h to have the correct prototype for A(). Then #include caller.h in both where you define A() and where you invoke it. This "works", due to C calling conventions, where the caller is responsible for pushing and popping arguments off the stack. In your case the caller doesn't push anything on the stack. When A() executes it will use whatever values are on the stack as its parameters, which in this case could be any value at all.

                  K Offline
                  K Offline
                  k5054
                  wrote on last edited by
                  #8

                  Further to this, in C the declaration void f(); does not declare a function with no parameters, but declares a function with an unknown number of parameters. If you know that a function takes no arguments, you should prototype it as void f(void);. Otherwise, the compiler will happily let you do the following:

                  void f();
                  int main(void)
                  {
                  f();
                  f(15);
                  f("Hello", "World");
                  }

                  Using gcc I can compile the above with -Wall -Wextra, and get no warnings. If I add -Wstrict-prototypes, I do get the warning "function declaration isn’t a prototype [-Wstrict-prototypes]. If you're using MS-VSC, there's probably a similar warning flag that you can use.

                  1 Reply Last reply
                  0
                  • S samzcs

                    I read a program, a function definition like:

                    void A( int channel)
                    {
                    }

                    While the caller like:

                    A(); //no parameter here.

                    I know it's OK. can't find a explanation.

                    Kornfeld Eliyahu PeterK Offline
                    Kornfeld Eliyahu PeterK Offline
                    Kornfeld Eliyahu Peter
                    wrote on last edited by
                    #9

                    There are only two options... 1. There is a function declarations in an other (probably h) file with a default value

                    // this is in an external h
                    void A(int c = 0);

                    // this is the main cpp
                    int main()
                    {
                    A(1);
                    }

                    // this is in an external cpp
                    void A(int c)
                    {
                    // empty
                    }

                    2. There is an overloaded version of A somewhere else

                    Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

                    "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                    D 1 Reply Last reply
                    0
                    • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                      There are only two options... 1. There is a function declarations in an other (probably h) file with a default value

                      // this is in an external h
                      void A(int c = 0);

                      // this is the main cpp
                      int main()
                      {
                      A(1);
                      }

                      // this is in an external cpp
                      void A(int c)
                      {
                      // empty
                      }

                      2. There is an overloaded version of A somewhere else

                      Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      Kornfeld Eliyahu Peter wrote:

                      void A(int c = 0);

                      Do C functions allow this?

                      "One man's wage rise is another man's price increase." - Harold Wilson

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                      Kornfeld Eliyahu PeterK 1 Reply Last reply
                      0
                      • D David Crow

                        Kornfeld Eliyahu Peter wrote:

                        void A(int c = 0);

                        Do C functions allow this?

                        "One man's wage rise is another man's price increase." - Harold Wilson

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                        Kornfeld Eliyahu PeterK Offline
                        Kornfeld Eliyahu PeterK Offline
                        Kornfeld Eliyahu Peter
                        wrote on last edited by
                        #11

                        Of course not - but that only means that OP does not know the difference between C and C++

                        Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

                        "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                        D 1 Reply Last reply
                        0
                        • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                          Of course not - but that only means that OP does not know the difference between C and C++

                          Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #12

                          Kornfeld Eliyahu Peter wrote:

                          Of course not

                          Right. So then why would you suggest to him that such a function was a possible explanation for his question?

                          "One man's wage rise is another man's price increase." - Harold Wilson

                          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                          Kornfeld Eliyahu PeterK 1 Reply Last reply
                          0
                          • D David Crow

                            Kornfeld Eliyahu Peter wrote:

                            Of course not

                            Right. So then why would you suggest to him that such a function was a possible explanation for his question?

                            "One man's wage rise is another man's price increase." - Harold Wilson

                            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                            Kornfeld Eliyahu PeterK Offline
                            Kornfeld Eliyahu PeterK Offline
                            Kornfeld Eliyahu Peter
                            wrote on last edited by
                            #13

                            As I told - I'm not sure OP knows the difference between C and C++ and what language is the project in (a leftover from someone else probably)... Taking in count, that it is part of code from a working system, and that the mentioned compiler can compile both C and C++, and the abilities of C and C++ I put my bet on OP's mistake... In fact if we stick to the C version, it can not work at all (C has only the varargs option, but that forces at least one named parameter, which we have not)...

                            Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

                            "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                            1 Reply Last reply
                            0
                            • S samzcs

                              It's a GHS compiler. An embedded project.

                              _ Offline
                              _ Offline
                              _Superman_
                              wrote on last edited by
                              #14

                              It's possible that the compiler patches the call with a default argument. What is the value in channel? I'm guessing it is 0. Anyway this is not valid in standard C and will surely not compile in GCC or Microsoft C compilers.

                              «_Superman_»  _I love work. It gives me something to do between weekends.

                              _Microsoft MVP (Visual C++) (October 2009 - September 2013)

                              Polymorphism in C

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups