473,396 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. void ButtonAction( Button *button, Mode mode )
  3. {
  4.     Button *b = (Button *)((button)->actions[(mode)];
  5.  
  6.     if( mode == RepeatPress ) // some enum val
  7.       *(b->actions[RepeatPress]) = EventMultiStepLeft; 
  8.  
  9.      //execute 
  10.      ...
  11. }
  12.  
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. typedef struct
  3. {
  4.     VOLATILE UINT32  type ;  
  5.     VOLATILE UINT32  polarity ;
  6.     VOLATILE UINT32  pin ; 
  7.     VOLATILE void *  actions [ NumButtonActions ] ;
  8. } Button ;
  9.  
  10.  
Mar 3 '11 #1
2 1814
donbock
2,426 Expert 2GB
On line 7 you store EventMultiStepLeft 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.
Mar 4 '11 #2
AH I needed to cast one more layer! I didn't see that, thanks...
Mar 4 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

51
by: BigMan | last post by:
Does the C++ standard define what should happen in case of NULL pointer dereferencing. If not, does it say that it is illegal? Where, if so, does it say it?
10
by: Denis Palmeiro | last post by:
Hello eveyone. What exactly is a NULL Pointer Dereference? I tried searching google but the only results that returned were bug reports. Some example code, if possible, would also be...
30
by: Michael B Allen | last post by:
I have a general purpose library that has a lot of checks for bad input parameters like: void * linkedlist_get(struct linkedlist *l, unsigned int idx) { if (l == NULL) { errno = EINVAL;...
5
by: Angel Tsankov | last post by:
Does the standard define what happens when a NULL pointer is dereferenced? If so, where?
51
by: atv | last post by:
Hi, Just to check, if i set a pointer explicitly to NULL, i'm not allowed to dereference it? Why is that, it's not like it's pointing to any garbage right? Why else set it to NULL. I can remember...
7
by: Lutz Richter | last post by:
Hi, given the following code: ----------------------------- class B { public: B(): Value(99) {} int Get() { if (this) return Value; else return -1; }
11
by: Alan Woodland | last post by:
Hi, I'm fairly sure this is undefined behaviour, despite the fact that it compiles and 'runs' (prints "this doesn't exist") on all my platforms: #include <iostream> class foo { public:...
17
by: Tony Jackson | last post by:
Hi I'm quite new to C programming - I have more of a Java background: maybe someone here can advise me. I'm nearly finishing a little program but it has some hard-to-find bugs that basically...
20
by: prashant.khade1623 | last post by:
I am not getting the exact idea. Can you please explain me with an example. Thanks
6
by: ma740988 | last post by:
Given the snippet. class foo { public: ~foo() { // type id should be able to get the type of the class ( I think ) std::cout << " foo destructing " << std::endl; } };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.