0

I have hit a wall now and would like to get back to speed.

I have a "fileA.c" file and "fileB.c" file that I would like to pass variables between. Furthermore, "fileA.h" and "fileB.h" are the headers respectively.

A variable UINT16 Storage1.CntLog1.PosEdge in "fileA.c" to "fileB.c". How do I do this using a pointer?

Below is code snippet of FileA.h header file and I would like to pass to another FileB.c the Variable PosEdge as a reference. There are 2 varibales PosEdge one in Header and the Other in c file. Anyone will do.

typedef struct                   
{
  UINT16   PosEdge;     
} S_PosEdge;

typedef struct S_CntLog1              
{
   S_PosEdge  CntLog1;        
} S_CntLog1;

This is a snippet of FileA.c

typedef struct                
{
   UINT16 PosEdge;    
} S_CntLog2;

PRIVATE S_CntLog1     Storage1; 
PRIVATE S_CntLog2   *Storage2 = NULL; 

Storage1.CntLog1.PosEdge = Storage2->PosEdge;

What I tried........ I tried using something like this

UINT16 ShareLog(void)
{
    return (Storage1.CntLog1.PosEdge);
}

and similarly declaring it in the header "fileA.h", then "#include fileA.h" in "fileB.c" and furthermore Blink an LED if (Storage1.CntLog1.PosEdge==0x01)

I have no idea how I can send this information using Pointers..... Any help would be appreciated. Thanks. maybe something along the lines of

UINT16* pntr1 =&Storage1.CntLog1.PosEdge;

But the PIC32 compiler doesn't like the "&"

newb7777
  • 517
  • 5
  • 22
  • Can you please provide code showing what you have tried? It would also be nice to know what compiler errors you are getting (if any). – embedded_guy Apr 10 '15 at 01:02
  • do not declare an instance of a variable in a header file. put the variable in the file global data space (I.E. outside of any function) and place an 'extern' statement in the appropriate header file. Note: global variables are a bad idea. Much better to write a getter and setter functions and call those functions to get/set the variable value. – user3629249 Apr 11 '15 at 11:49
  • there is no 'PRIVATE' in c, even c++ is 'private' please let up know what your actual code looks like – user3629249 Apr 11 '15 at 11:54
  • strongly suggest do not typedef a struct definition. it leads to confusion, clutters the code, clutters the compiler name space and makes debug and later maintenance much more difficult – user3629249 Apr 11 '15 at 11:56
  • thanks for your comments user 3629249. Believe it or not, this code was written by a very senior person and not in age. I am pure analog/Verilog HW person picking up on embedded Software. I guess studying on code that has a bad programming form. – newb7777 Apr 12 '15 at 19:55

1 Answers1

1

One possible way would be a common header file, i.d. common.h which you include in both "fileA.c" and "fileB.c"

#ifndef COMMON_H
#define COMMON_H

extern UINT16 *global_pulse_count;

#endif /*COMMON_H*/

In file "fileA.c" you have then to add two things:

1) reserve memory for your global variable (outside any function body):

UINT16 *global_pulse_count; (note there is no external here.)

2) within some appropriate function (you will have an init() function for sure)

global_pulse_count = &Pulse.count; (Assign the address to the global pointer variable.)

Afterwards you can access (*global_pulse_count) for reading and writing from both "fileA.c" and "fileB.c".

HTH

  • HTH, I will try this but it would be good to have a way of doing this without creating a global variable. Thank you.. – newb7777 Apr 09 '15 at 20:14
  • As I wrote, this is one possible way to achieve the result you (probably) desire. There are several others: (1) write a getter and a setter function for pulse count in "fileA.c" and make the function headers known to "fileB.c" (2) write a get pulse count pointer function ...... – Samuel Abundash Apr 09 '15 at 20:20
  • For some strange reason with PIC micro, it doesn't like the "&" operator. – newb7777 Apr 09 '15 at 21:15
  • 1
    I'm not familiar with that particular compiler, but `&Pulse.count` is standard C and thus supposed to work, provided your struct variable (not type!) `Pulse` has a member `count`. Also note that C is case sensitive. Maybe, you like to provide some code to show what is actually working and what isn't. – Samuel Abundash Apr 09 '15 at 21:24
  • So I put the new code in and tried a few other things but no success. – newb7777 Apr 10 '15 at 18:53