Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Minor irritation with void pointers

Credits go to Steve Terry for helping out!
This thread is locked; no one can reply to it. rss feed Print
Minor irritation with void pointers
Ju-Han Soon
Member #6,738
December 2005

using Dev C++ here... coding in C.

I'm using install_param_int here, and it accepts a void pointer as an arguement. If I send an int pointer as the arguement, it works but I've got install_param_int in quite a few places. This creates a hell alot of Warning signals from the compiler and it floods the compiler error screen so much that I find it hard to tell my real problems sometimes.

So i just created a void *pointer, pointed it at the address of the int, and used that as the arguement. Now I got a different warning "assignment discards qualifiers from pointer target type".

Can anyone tell me how to disable specific warnings for Dev C++ (or MingW... i duno which is the culprit)? Its really bugging me.

Thanks
=Han=

Steve Terry
Member #1,989
March 2002
avatar

Typecasting your int to a void pointer should work. (void*)&my_int? but that doesn't seem right.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Fladimir da Gorf
Member #1,565
October 2001
avatar

Compile the project in C, not C++.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Simon Parzer
Member #3,330
March 2003
avatar

Steve Terry said:

Typecasting your int to a void pointer should work. (void*)&my_int? but that doesn't seem right.

Why doesn't it seem right? It's the only way if you're using C++!

BAF
Member #2,981
December 2002
avatar

int myint = 42;
install_param_int(..., (void *)&myint);
...
void mything(void *param)
{
   int theint = (int *)param;
//   theint should == 42
}

Ju-Han Soon
Member #6,738
December 2005

Thanks! I just needed to know about (void*)&my_int thats all... and it worked! no more warnings.

=Han=

Kitty Cat
Member #2,815
October 2002
avatar

Assuming myint won't leave scope while the timer is running (eg. a local variable). If it does, your program will crash when it tries to access invalid memory. For example:

int main()
{
  int mynum;
  allegro_init();
  install_timer();
  intall_param_int(myfunc, 10, (void*)&mynum);
  install_keyboard();
  install_mouse();
  set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);

  rest(1000);
  return 0;
}

Here, main will return and invalidate mynum, which the timer can still try to use before Allegro's exit handlers properly stop and close the timer functions. As well, changing mynum in main will change the value in the timer (and vice versa).

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Go to: