I have function pointer, that is declared as follows in file 1.h
// file : 1.h
typedef void (*my_func_ptr_T)(int , int);
In file 1.c I create a global instance and initialize it
// file 1.c
my_func_ptr_T func;
void callback (my_func_ptr_T _ptr) {
func = _ptr;
}
Now, how can i use this function ptr 'func' in another file say 2.c
// file 2.c
void do_callback() {
// i want to do this
func(1,2);
}
Check the below changes
// file : 1.h
typedef void (*my_func_ptr_T)(int , int);
extern my_func_ptr_T func;
//file 1.c
#include "1.h"
//func is visible
// file 2.c
#include "1.h"
//func is visible
Changing 2.c to below will help. Leave 1.h and 1.c unchanged.
#include "1.h"
extern my_func_ptr_T func;
void do_callback() {
func(1,2);
}
//sample sum function
void sum( int a, int b ) {
printf ("SUM : %d\n", a+b);
}
//main
int main() {
func = sum;
do_callback( );
}
It is important to understand that, keyword extern doesn't define the variable. It is just a variable declaration. No memory is allocated for an extern variable declaration. The func variable is actually defined in 1.c and that is where memory is allocated. You are free to use extern declarations in as many files ( for e.g. 3.c) as you need.
Note: Careful when adding extern to header file as in accepted answer. That makes variable visible, in which ever source file the header in included and so a potential risk of name collisions or unintentional modifications.
You make the variable func known in file 2.c:
// file 2.c
extern my_func_ptr_T func;
void do_callback() {
// i want to do this
func(1,2);
}
By declaring funcas extern you tell the compiler that this particular variable will live in a file / different compilation unit (e.g. 1.o compiled from 1.c, where a variable of the same name and type is declared without the externkeyword), and it does not need to create it when compiling 2.c into 2.o.
This is an example about using function pointers...
#include <stdio.h>
void prev(int x)
{
printf( "%d\n", x-1 );
}
void next(int x)
{
printf( "%d\n", x+1 );
}
typedef void (*my_func_ptr_T)(int);
int main()
{
my_func_ptr_T foo;
foo = &prev;
foo( 5 );
return 0;
}
You can run the code in this DEMO
Related
I have a problem about a program. I bet that it has to do with the fact that I use static. Here is my t.h
static int cnt;
void f();
my main.c
#include <stdio.h>
#include "t.h"
void main()
{
cnt=0;
printf("before f : cnt=%d\n",cnt);
f();
printf("after f : cnt=%d\n",cnt);
}
and finally my f.c
#include "t.h"
void f()
{
cnt++;
}
The printf prints cnt=0 both times. How is this possible when I do cnt++? Any ideas?
Thanks in advance
In C, static means "Local to the module"
Take note, that the #include statements just pastes the header file in the including file.
therefore, you are creating two distinct symbols (happens to have the same logical name) in different modules.
f.c cnt is a different cnt then main.c
Note:
static in C has different meaning then its C++ counterpart.
and because C++ is C Compatible, static outside a class have the same meaning as in C
Edit:
In your case, you don't want a static you want a variable, but i guess you had problem with the Linker telling you about "ambiguous symbols".
I would suggest to declare an extern in the header file, and declare the actual variable in a module.
t.h
extern int cnt; // declaration of the variable cnt
main.cpp
#include
#include "t.h"
void main()
{
cnt=0;
printf("before f : cnt=%d\n",cnt);
f();
printf("after f : cnt=%d\n",cnt);
}
t.cpp
#include "t.h"
int cnt = 0; // actual definition of cnt
void f()
{
cnt++;
}
Data should not be defined in the header files.
In your example you will create a separate copy of that static variable in every compilation module which includes this .h file.
Don't define cnt in your header file. Instead, define it in f.c:
#include "t.h"
int cnt = 0;
void f(){
cnt++;
}
Then in main.c, add the following before the beginning of your main function:
extern int cnt;
Given 2 files, for examples:
file1.c :
int main(){
f();
return 0;
}
file2.c:
void f(){
return;
}
Why I can't call f from file1.c like that?
Because first you need to tell the compiler (declare) that it exists somewhere:
void f(); //function declaration
int main()
{
f();
return 0;
}
Usually, though, it is better to put such declarations in a separate header file (e.g. file2.h) so that later you could include this file (e.g. #include "file2.h") instead of duplicating such declaration in every other file where you need this function.
The problem is that file1.c does not "know" that the function f exists. You need to use a prototype. The standard way is to put prototypes in header files and definitions in .c files.
It could look like this:
file1.c:
#include "file2.h"
int main(){
f();
return 0;
}
file2.h:
#ifndef FILE2_H
#define FILE2_H
void f();
#endif
file2.c:
#include "file2.h"
void f(){
return;
}
Why we don't use extern when using function from one .c file in another .c file , but we must do extern for variables case? Is it related to linker?
Functions are extern qualified by default (unless you change it to internal with static). For example,
int func(void) {
}
extern int func2(void) {
}
Both func and func2 are external. The extern keyword is optional for external functions.
Actually, function names act just like variable names, but function prototypes are extern by default.
From cpprerefence:
If a function declaration appears outside of any function, the identifier it introduces has file scope and external linkage, unless static is used or an earlier static declaration is visible.
you can create a .hfile,declare functions you want to use in the other .c files and #include the .hfile in the other .c files.
Demo program,
one.c
#include "one.h"
void func1() //defination
{
//code
}
one.h
void func1(); //declaration
main.c
#include <stdio.h>
#include "one.h"
int main()
{
func1();
}
Then compile program in Gcc Linux : gcc main.c one.c
Yes, Let consider you have one .c file as process.c and you declared it in process.h . Now if you want to use the function from process.c to suppose tools.c then simply #include "process.h" in tools.c and use ther function. The process.h and process.c file should be in your project.
process.c file
#include<stdio.h>
#include<conio.h>
#include "process.h"
unsigned int function_addition(unsigned int a, unsigned int b)
{
unsigned int c = 0;
c = a + b;
return c;
}
process.h:
<bla bla bla >
unsigned int function_addition(unsigned int a, unsigned int b);
<bla bla bla >
tools.c file:
#include<stdio.h>
#include<conio.h>
#include "process.h"
my_tools()
{
unsigned int X = 1, Y = 9, C = 0;
C = function_addition(X,Y);
}
All these files are in one project.
i have 2 source files (.c) named file1.c and file2.c which need to share between them a variable,
so that if in one source file the variable is been updated then in the other sourced file when accessing this variable the change will be seen.
what i did is create another source file called file3.c and header file called file3.h (which, of course, is been included in file1.c file2.c and in file3.c)
in file3.c:
int myvariable = 0;
void update(){//updating the variable
myvariable++;
}
int get(){//getting the variable
return myvariable;
}
in file3.h:
extern int myvariable;
void update(void);
int get(void);
in file1.c:
.
.
.
printf("myvariable = %d",get());//print 0
update();
printf("myvariable = %d",get());//print 1
.
.
.
in file2.c:
.
.
.
printf("myvariable = %d",get());//print 0 but should print 1
.
.
.
but the problem is when in file1.c update is invoked and myvariable is updated
the change cannot be seen in file2.c because in file2.c when get is invoked and
myvariable is printed then 0 is been printed, only if in file2.c update is invoked then the change is been seen.
it seems like the variable is shared but for each source file there is a different variable value/different memory for this variable
You can declare the variable as extern in the others file when you need the variable ...
include file3.h in file1.c and file2.c
I recommend to avoid extern variables because of code clutter - repeating externs in each and every file using that global. It is usually better to bind global variable to some file scope by making it static. And then to use interface functions to access it. In your example terms it will be:
// in file3.h
void update(int x);
int get(void);
// in file3.c:
static int myVariable = 0;
void update(int x){
myVariable = x;
}
int get(){
return myVariable;
}
// in other files - include file3.h and use
// update() / get() to access static variable
Here is just one possible solution. Doing this the the variable isn't global to the entire application and can only be read/written to using the access functions. Please let me know if you have questions.
Files: access.c access.h file2.c main.c
Compile with: gcc main.c file2.c access.c -o test
Run: ./test
File: main.c
#include <stdio.h>
#include "access.h"
int main( int argc, char *argv[] )
{
int value;
put( 1 );
printf("%d\n", get());
put( getValue() + 1 );
printf("%d\n", getValue());
return(0);
}
File: access.c
#include "access.h"
static int variable = 0;
int get( void )
{
return(variable);
}
void put( int i )
{
variable = i;
return;
}
File: file2.c
#include <stdio.h>
#include "access.h"
int getValue( void )
{
int i = get();
printf("getValue:: %d\n", i);
put(++i);
printf("after getValue:: %d\n", get());
return( i );
}
File: access.h
extern int getValue( void );
extern int get( void );
extern void put( int i );
And here is the run output:
[root#jrn SO]# ./test
1
getValue:: 1
after getValue:: 2
getValue:: 3
after getValue:: 4
4
I hope this helps.
I have two C files. I want to declare a variable in one, then be able to access it from another C file. My definition of the example string might not be perfect, but you get the idea.
//file1.c
char *hello="hello";
//file2.c
printf("%s",hello);
// file1.h
#ifndef FILE1_H
#define FILE1_H
extern char* hello;
#endif
// file1.c
// as before
// file2.c
#include "file1.h"
// the rest as before
*hello in file1.c must be declarated global and extern in file2.c must be global too (not inside function)
//file2.c
extern char *hello;
... function()
{
printf(...)
}
What you have would work. What you want to research is "linkage" in C. Basically objects not within a function or marked as static are extern (think global). What you need to do in this case is notify the compiler that there is in fact a variable called hello defined elsewhere. You do this by adding the following line to file2.c
extern char* hello;
this works
t.c
#include <stdio.h>
int main(void)
{
extern int d;
printf("%d" "\n", d);
return 0;
}
h.c
int d = 1;
output
[guest#localhost tests]$ .ansi t.c h.c -o t
[guest#localhost tests]$ ./t
1
[guest#localhost ~]$ alias .ansi
alias .ansi='cc -ansi -pedantic -Wall'
[guest#localhost ~]$
file1.c
int temp1=25;
int main()
{
.
.
}
file2.c
extern int temp1;
func1();
func2(temp1);
temp1 is defined in file1.c .when you want to use it in file2.c you must write
extern int temp1;
you must do it in each file which you want to use this variable
file_2.c
#include<stdio.h>
int count;
void extern_function();
void main()
{
count = 5;
extern_function();
}
file_3.c
#include<stdio.h>
void extern_function()
{
extern int count;
printf("the value from the external file is %d",count);
}
Now run the code
$gcc file_2.c file_3.c -o test
$./test
It works!!