void pointer as a parameter
Mar 14, 2013 at 12:33am UTC
I want to have a function that has a pointer to an int/double/string so I thought I'd use a void pointer like so:
1 2 3
int someFnc(int a, int b, const void *key){
// take care of converting key into appropriate type in here
}
And when I want to use this function I'd like to be able to do something like this:
1 2 3 4 5
main{
...
int myKey;
someFnc(1,2,myKey);
]
But I get a compiler error telling me:
invalid conversion from 'int' to 'const void' -[fpermissive]
Do I need to convert myKey into a void pointer before passing it as an argument?
Edit: Why does passing myKey like this work?
someFnc(1,2,&myKey);
Last edited on Mar 14, 2013 at 12:34am UTC
Mar 14, 2013 at 1:29am UTC
Since the parameter is a void * you need to pass in the address of something. If you cast an integer into a void * it would most likely be an invalid pointer.
Topic archived. No new replies allowed.