My question is about Memory and how it is being accessed across a program with multiple C files (Multiple Modules).
file1.h Version 1
#ifndef file1_h
#define file1_h
typedef struct
{
UINT8 var1;
UINT16 var2;
UINT16 var3;
} TestAccess;
static TestAccess* pLongRangeAccess;
#endif
file1.h Version 2
#ifndef file1_h
#define file1_h
typedef struct
{
UINT8 var1;
UINT16 var2;
UINT16 var3;
} TestAccess_t;
TestAccess_t* pLongRangeAccess;
#endif
Main.c
#include "file1.h"
void main(void)
{
pLongRangeAccess->var1 = 4;
pLongRangeAccess->var2 = 8;
pLongRangeAccess->var3 = 16;
}
Module1.c //Needs read/write access to the struct variables
#include "file1.h"
void module1(void)
{
pLongRangeAccess->var1 = 5;
pLongRangeAccess->var2 = 10;
pLongRangeAccess->var3 = 20;
}
Question 1) See "Version1" Does static in keyword make it such that there will be only one copy in heap memory (uninitialized) and therefore only one address of the pointer variable OR would it create 2 static variables with different memory since the header is used in every module?
Question 2) see "Version 2" If static was not declared for the pointer variable, then it would still be in heap memory and there would be one UNIQUE address that could be used to accessed to read and write operations to the members?
Question 3) In version 2 of file1.h if the pointer was declared in Main.c, and the memory location was fixed i.e it was declared as a constant, would it be available for access from any module by de-referencing the memory address location?
staticmeans static storage duration and linkage, read it as a global that's private to the compilation unit. What you're probably looking for isextern