Thread: Help....Trying to use extern / static to no avail....

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    91

    Help....Trying to use extern / static to no avail....

    I have four global variables defined before main in file1.c
    Code:
    boolean moreThanOneSwitch = F, moreThanOneSwitchTwoSec = F;unsigned int switchHits[12] = {0}, button = 0;

    I declare these variables 'extern' in file1.h
    Code:
    extern unsigned int switchHits[12], button;extern boolean moreThanOneSwitchTwoSec, moreThanOneSwitch;
    I place them into a struct in file1.h
    Code:
    struct swParams {    unsigned int switchHits[12];
        unsigned int button;
        boolean moreThanOneSwitch;
        boolean moreThanOneSwitchTwoSec;
    };
    file1.h also contains a declaration to a function that returns that struct so that main can act upon the results.
    Code:
    struct swParam swMath(unsigned int *p);
    Within main I call the function which happens to exists in file2.c
    This function gets called on a timer and modifies the variables
    After returning from the function in main I look at these variables and decide whether or not (through logic) on how to proceed. At different points within the logic I may or may not clear the variables. (I do not want to clear them within the function)....Can someone please point me in the right direction on how to do this?? I'm very lost here.....Basically I want to initialize variables in main, call a function periodically that can modify these variables, use them in main and allow main to also possibly modify them....If I place everything in main all works fine

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ridgerunnersjw
    I place them into a struct in file1.h
    No, you didn't. You declared a struct that happens to have four members of the same names and types as those four global variables, but which have nothing to do with the global variables.

    Since it is clear that you are insistent on having global variables even though I am not convinced that they are necessary for the problem that you're trying to solve, I'll just suggest this:

    In file1.h:
    Code:
    struct swParams {
        unsigned int switchHits[12];
        unsigned int button;
        boolean moreThanOneSwitch;
        boolean moreThanOneSwitchTwoSec;
    };
    
    extern struct swParams switchParams;
    
    void swMath(unsigned int *p);
    Notice that I declared swMath as returning void. You do not want to return a struct swParams object because the one that you're working with is global. But honestly, I still don't see why you need a global variable. You can declare swMath like this instead:
    Code:
    void swMath(struct swParams *switchParams, unsigned int *p);
    then in the main function:
    Code:
    struct swParams switchParams;
    swMath(&switchParams, x);
    Anyway, if you insist on a global variable, then in file1.c:
    Code:
    #include "file1.h"
    
    struct swParams switchParams = {{0}, 0, F, F};
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    Thank you! I look forward tomorrow giving your stuff a try.....It's complicated from this end because this is embedded code on a microprocessor so hardware ISRs are constantly going off and both main and the function have the opportunity to change the states of the booleans....It all depends on user input from switches....I am constantly scanning the switches and running the function every 2 seconds or so and then in main I act upon these switches and transmit RF data....depending on the switches thrown main might make changes to the boolean states...I cannot clear them within the function .....

  4. #4
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    I just want to say Thank you! The code examples you sent work great.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-25-2017, 10:25 AM
  2. does a variable can be a static and extern both ?
    By gunjansethi in forum C Programming
    Replies: 2
    Last Post: 03-09-2011, 08:51 AM
  3. static global variable and static extern global variable
    By gunjansethi in forum C Programming
    Replies: 8
    Last Post: 01-12-2011, 01:00 AM
  4. Difference Betn Volatile,static,extern variables
    By nitinmhetre in forum C Programming
    Replies: 3
    Last Post: 12-26-2006, 07:13 AM
  5. extern static?
    By JadedProgrammer in forum C++ Programming
    Replies: 8
    Last Post: 08-16-2005, 12:11 PM

Tags for this Thread