Help with a pointer. Dereferencing a null pointer issue.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode2
    New Member
    • Apr 2009
    • 32

    Help with a pointer. Dereferencing a null pointer issue.

    Hi

    This is kind of a noob C question although I'm not really one anymore.

    I have this simple function which processes a button press:

    Code:
    void ButtonAction( Button *button, Mode mode )
    {
        Button *b = (Button *)((button)->actions[(mode)];
    
        if( mode == RepeatPress ) // some enum val
    	  *(b->actions[RepeatPress]) = EventMultiStepLeft; 
    
         //execute 
         ...
    }
    where "actions" is an array of function pointers of an event to send upon a button press. All the code I listed in the function is new (except executing the event); I just put it there, but I get an error: "invalid use of void expression"

    But, why is it null? I cast it.

    Button type looks like this:

    Code:
    typedef struct
    {
        VOLATILE UINT32  type ;  
        VOLATILE UINT32  polarity ;
        VOLATILE UINT32  pin ; 
        VOLATILE void *  actions [ NumButtonActions ] ;
    } Button ;
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    On line 7 you store EventMultiStepL eft in the location pointed to by b->actions[RepeatPress]. However, actions is defined as an array of void*. The compiler doesn't know how to store a value via these pointers because it doesn't know what type they point at. I'm surprised your compiler message refers to a "void expression"; that isn't very helpful.

    You either need to cast the pointer before dereferencing it or consider changing the definition of actions into an array of unions of pointers to the various things that can be pointed at.

    Line 4 is missing a close-parenthesis, but I assume that is just a transcription error.

    In general, I would explicitly verify that all of these pointers were not NULL before dereferencing them (lines 4 and 7), but that wouldn't explain this error message.

    Comment

    • dissectcode2
      New Member
      • Apr 2009
      • 32

      #3
      AH I needed to cast one more layer! I didn't see that, thanks...

      Comment

      Working...