Global Pointer and structures

This is a discussion on Global Pointer and structures within the C Programming forums, part of the General Programming Boards category; Hello, I'm used to creating objects in C++ and returning the value of private variables using public functions for other ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    3

    Global Pointer and structures

    Hello,

    I'm used to creating objects in C++ and returning the value of private variables using public functions for other tasks to access.

    Since I'm working in C, I am trying to do something similar. I realize I could just use a global variable to pass to other tasks, but using a function to return the value makes things easier to understand where the value is coming from for someone else viewing the code (in my opinion at least).

    I am using a function that solely returns a value a global pointer is pointing to.

    Code:
    u8 Get_Go_State(void)
    {
        return (Global_Button_State_Pointer->Rocker_GO);
    }
    This is how I declare the structure and pointer.

    Code:
    // .h
    typedef struct Rocker_Switch {
                u8 Rocker_GO;
                u8 Rocker_STOP;
    } Rocker_STOP_GO;
    
    
    extern Rocker_STOP_GO *Global_Button_State_Pointer;
    
    //.c
    Rocker_STOP_GO *Global_Button_State_Pointer;
    
    
    void Button_Task_Run (void)
    {
        Rocker_STOP_GO Rocker_State;
        Global_Button_State_Pointer = &Rocker_State;
    ...
    }
    While in the button task, I receive the proper values when I run the return function, but when I'm in a different task, I only get a 4 when the return function is called. How do I get the actual value stored in Rocker_State.Rocker_GO to a different task?

    I've tried searching for solutions, but found none.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    Rocker_STOP_GO *Global_Button_State_Pointer;
    
    void Button_Task_Run (void)
    {
        Rocker_STOP_GO Rocker_State;
        Global_Button_State_Pointer = &Rocker_State;
    ...
    }
    "Global_Button_State_Pointer" is a global pointer, but "Rocker_State" is a local variable. Thus "&Rocker_State" is the local address which is invalid when you quit the function.

    You would need a global structure if you want to access it from everywhere and don't want to pass it as a parameter.

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    Quote Originally Posted by AndiPersti View Post
    [code]

    "Global_Button_State_Pointer" is a global pointer, but "Rocker_State" is a local variable. Thus "&Rocker_State" is the local address which is invalid when you quit the function.

    You would need a global structure if you want to access it from everywhere and don't want to pass it as a parameter.

    Bye, Andreas
    I see. That makes a lot of sense. I've been having trouble making a global structure and any information I could find used that format of pointer to structure. How do I declare the structure as a global? I get weird errors when trying to place extern in front.

    Thank you so much for your help. It really is greatly appreciated. I've been tinkering with this for way too long.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    1. I realized saying "I get weird errors" and not supplying the errors and my code for review is worthless. Lawl.

    2. I figured it out on my own! Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. not sure how to set static global pointer
    By itsthemac in forum C Programming
    Replies: 5
    Last Post: 05-10-2011, 01:12 AM
  2. global structures
    By yohanevindra in forum C Programming
    Replies: 5
    Last Post: 05-20-2010, 08:28 PM
  3. Global pointer
    By misterowakka in forum C++ Programming
    Replies: 1
    Last Post: 01-22-2008, 09:50 AM
  4. Please help... Global structures
    By msmith in forum C Programming
    Replies: 5
    Last Post: 09-16-2007, 06:18 PM
  5. global pointer problem?
    By LiLgirL in forum C Programming
    Replies: 7
    Last Post: 11-25-2003, 03:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21