Passing pointer to function taking no parameters

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Using Atmel Studio 6.2.1563 Sp2

 

Is it valid in C (including C99) to pass a pointer of any type to a function that is declared as having not arguments/parameters?

 

This compiles with no errors or warnings

 

int bar = 2;

void foo()
{
    bar++;
}

int main(void)
{
    int crap = 5;
    int* ola = &crap;

    while(1)
    {
        foo(ola);
    }
}

 

I would expect an error such as "too many arguments for function 'foo'"

 

This topic has a solution.
Last Edited: Wed. Nov 4, 2015 - 11:29 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well you didn't post your avr-gcc command line, so I'm guessing you do have -Wall -Wextra in there. You would think that would enable ALL warnings, however this isn't the case. You have to add certain specific warnings separately. I add -Wstrict-prototypes when compiling my code.

 

Doing that generates a warning:

 

freaks1.c:53:6: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
 void foo()

 ^

 

OK so we modify the line to void foo (void) and compile again:

 

freaks1.c:60:2: error: too many arguments to function ‘foo’
  foo(ola);
  ^

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for taking the time to sort that out for me!

 

So by omitting 'void' in our function prototypes and definitions we shooting ourselves in the foot?

Adding a prototype of sorts like this has the same problem; compiles with no error.

#include <avr/io.h>


int bar = 2;

void foo();

int main(void)
{
    int crap = 5;
    int* ola = &crap;

    while(1)
    {
        foo(ola);
    }
}


void foo()
{
    bar++;
}

 

 I was my understanding that declaring a function as

void foo();

was short hand for and equivalent to

void foo(void);

but that seems to be wrong. Maybe its a C++ corruption of my memory.

 

In any case, it sure seems like something worthy of a warning with -Wall
 

Last Edited: Fri. Sep 18, 2015 - 11:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Maybe its a C++ corruption of my memory.

 

Yes, it is.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I assume that calling foo(ola) puts the pointer ola on the stack when it calls foo().  Since foo() never uses ola, the pointer ola just gets ignored by foo().  When foo() returns, the stack pointer goes back to what it was before foo() was called.   The address of an variable is two bytes regardless of the size of the variable (in the AVRs with 64K flash or less).

 

Try doing it with a variable that is not an address.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
void foo()

This gives you a "legacy style" function declaration, which needs to be accepted by the compiler because "legacy code." :-(  You can turn it off with the -Wstrict-prototype switch.

As Simonetta implies, the arguments get put someplace where they don't bother anything if they're not actually used, and functions can return values whether or not the caller uses the return value (the caller using the return value when the function DOESN'T return one is more of a problem.)   Whether this actually uses "the stack", "a stack", or something else is implementation dependent.