I have hello.c containing a function hello():
#include <stdio.h>
static void hello() {
printf("hello.\n");
}
Now, I have main() in main.c, from which I want to call hello() residing in hello.c.
I think I have to pass a function pointer from hello.c to a function in main.c, but I don't know how how to do that exactly. An example with an explanation on how to link the two files would be great! Thanks.
Like this?
hello.h
#ifndef INCLUDED_HELLO_H
#define INCLUDED_HELLO_H
void (*get_hello(void))(void);
// or, better:
// typdef void(*funcptr)(void);
// funcptr get_hello(void);
#endif
hello.c
#include "hello.h"
#include <stdio.h>
static void hello(void) {
puts("Hello!");
}
void (*get_hello(void))(void) {
return hello;
}
main.c
#include "hello.h"
int main(void) {
void(*hello)(void) = get_hello();
hello();
get_hello()();
}
EDIT:
As was quite correctly pointed out to me (sorry, I am new here), this is not exactly self-explaining. Well, the confusing bit is really only the syntax. What we're dealing with here are function pointers. They are exactly what it says on the tin: Pointers to functions, through which functions may be called. The syntax is off-putting, which is why typedefs are your friend, but there you are.
hello in main is a function pointer variable; it's a pointer to a function returning nothing and taking nothing.
get_hello is a function taking no arguments returning a pointer to a function returning nothing and taking no arguments. Similarly,
`int (*foo(double))(float);
would delare a function foo taking a double and returning a pointer to a function taking a float and returning an int.
And, well, that's all there is to it. It is, in principle, very simple, and only the horrible syntax makes it look complicated.
Related
I am pretty new to coding. I used a simple code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
Sayhi();
return 0;
}
void Sayhi()
{
printf("hi");
}
So when I compile the code it says function "sayhi" was not declared in this scope.
I even tried a different code which used "void" as a function but it didn't work.
This should work - simply declare and define "Sayhi()" before you use it:
#include <stdio.h>
#include <stdlib.h>
void Sayhi();
{
printf("hi");
}
int main()
{
Sayhi();
return 0;
}
A "better" approach would be to create a prototype for "Sayhi()":
#include <stdio.h>
#include <stdlib.h>
void Sayhi(void);
int main()
{
Sayhi();
return 0;
}
void Sayhi();
{
printf("hi");
}
Q: So what's a "prototype"?
https://www.programiz.com/c-programming/c-user-defined-functions
A function prototype is simply the declaration of a function that
specifies function's name, parameters and return type. It doesn't
contain function body.
A function prototype gives information to the compiler that the
function may later be used in the program.
Prototypes should always list the function's parameters. If no parameters, it should list "void".
The value of prototypes shines as your application increases in size and complexity. You'll want to move code OUT of "main()" and into separate .c source files (e.g. "mycomponent.c") and corresponding header files (e.g. "myheader.h").
One additional note: you should always NAME the variables in your prototypes (e.g. void myfunc(int i);.
Q: Do you understand why you were getting the compile error (the function needed to be declared somehow before you used it), and how you can fix it?
I have done this quiz, and do not understand the output
#include <stdio.h>
int main()
{
void demo();
void (*fun)();
fun = demo;
(*fun)();
fun();
return 0;
}
void demo()
{
printf("GeeksQuiz ");
}
Expected: Compiler error because I thought that normally demo() would need to be initialized before the call in main()?
Actual results: GeeksQuiz GeeksQuiz
Is my assumption wrong that functions generally need to be defined before they can be called?
functions generally need to be defined before they can be called
Well, not actually, compiler just needs to see a prototype before the call (usage). A forward declaration would be enough.
In your case, inside main(),
void demo();
is serving that purpose. Note that, this is not a function call.
So I'm new to c and wrote some code but i'm not sure why i'm getting an error when i try to run it.
int GlobalVariable=0;
int main()
{
int LocalVariable=0; //can be used within main()
dis();
printf(GlobalVariable);
return 1;
}
int dis()
{
GlobalVariable=5; //Can be accessed in any functions and made changes to it
}
Here is the prototype of printf function:
int printf(const char * restrict format, ...);
And look what you are typing:
int GlobalVariable=0;
printf(GlobalVariable);
The problem is that you used a function without first telling the compiler about it.
In this case you must provide function prototype as the function definition itself is provided after main
int dis( void ); // function prototype
int main()
{
...
}
int dis() // function definition
{
...
}
Alternatively, you can put the function definition before main(). But usually it would be better to have function prototypes before main() and (usually) even better to put the prototypes in a separate header file - that way it'd be easier to look straight into the main program without being bother about other function details.
#include <stdio.h>
void m();
void n() {
m();
}
void main() {
void m() {
printf("hi");
}
}
On compiling, an error
"undefined reference to m"
is shown. Which m is being referred to?
First, let me declare clearly,
Nested functions are not standard C. They are supported as GCC extension.
OK, now, in your code, m() is a nested function inside main(). It is having block scope for main() only. Outside main() other functions cannot see the existence of m() ,neither can call m() directly. m() can be called only inside main().
In your case, the call to m() inside n() is causing the issue. Even if you provided the forward declaration as void m();, linker won't be able to find the definition of m() and throw error.
Solution: Move the definition of m() outside main(), then you can use it from any other function.
Also note, the recommended signature of main() is int main(void).
As has been explained elsewhere, C doesn't support nested functions as a rule (gcc does as an extension, but almost no other compiler that I know of does).
You need to move the definition of m outside of main. Preferably you should define m before it is used by n:
#include <stdio.h>
void m()
{
printf("hi\n");
}
void n()
{
m();
}
int main( void ) // void main() is not a valid signature for main
{
n(); // call n, which calls m, which prints "hi"
return 0;
}
all. I'm currently working with an old established code base for a new project which is basically undocumented (i.e. averages 1 one-line comment per file). I just ran into something which I haven't seen before and am not quite sure how to interpret.
Firstly, they define a function type and a function in the header file (.h) in the form:
typedef void (*SOME_FUNCTION)(void *data, EXECUTION_CONTEXT *ec);
void add_function(SOME_FUNCTION aFunction, void *data);
In the main source file (.c), there is a function defined:
void add_function(void (*f)(void *data), void *data)
{
(Some code here)
}
So okay, there's a function pointer... but what the heck happened to the second argument, ec? Why would someone use a code design like this? For reference, when the function add_function is used, it is used in this sort of form:
void passedFunction(void *data, EXECUTION_CONTEXT *ec)
{
(Stuff the function does.)
}
void CallingFunction()
{
data = (some data stuff);
add_function((SOME_FUNCTION)passedFunction, data);
}
So, as you can see, the passed functions use the correct form that fits the original SOME_FUNCTION argument signature, but the definition for the add_function arguments is short by one argument.
Formally, the results are undefined: you are only permitted to call a function via a function pointer if the types match.
As for what actually happens, it depends on the calling convention and what the function does with the arguments. Chances are, the results aren't going to be good.
To add on James' answer:
Since the default calling convention is most likely cdecl, the call site is responsible for cleaning up the stack after passedFunction returns. Since the call site knows that it passed just 1 argument to the callee, the compiler can clean up the stack normally (even though technically this is undefined behavior).
Change the calling convention on passedFunction to stdcall though, and you 're in for some fireworks.
From the example code below, it doesn't appear that the variable(s) that are defined in the function pointer matters during the check. The code below compiles without warning.
#include <stdio.h>
int temp(int (*m)());
int main(int argc, char *argv[]) {
return temp(main);
}
int temp(int (*m)()) {
return 1;
}
However, the code below throws an error.
#include <stdio.h>
int temp(void (*m)());
int main(int argc, char *argv[]) {
return temp(main);
}
int temp(void (*m)()) {
return 1;
}
Due to this; it seems that the compiler (at least in my case GCC) only cares what the return value of the function pointer will be. The interesting thing here is that you CAN send the parameters correctly but if you do NOT specify the parameter (in our example m()), then the variables in m() when called will be junk.