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(...)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With