I was playing a bit with external variables and was wondering why I cannot access an external structure from outside functions?
Here's my code:
a.h
struct testing {
unsigned int val;
const char* str;
testing(unsigned int aVal, const char* aStr) : val(aVal), str(aStr){};
}
extern testing externalStruct;
a.c
#include "a.h"
testing externalStruct(10, "test");
test.c
#include <iostream>
#include "a.h"
unsigned int valCopy = externalStruct.val;
const char* strCopy = externalStruct.str;
int main()
{
std::cout<<"Direct val : "<<externalStruct.val<<std::endl; // Working, print 10
std::cout<<"Direct str : "<<externalStruct.str<<std::endl; // Working, print "test"
std::cout<<"Copy val : "<<valCopy<<std::endl; // Print 0 instead of 10
std::cout<<"Copy str : "<<strCopy<<std::endl; // Print nothing
return 0;
}
Any idea?