In the past couple of days i have noticed that a lot of people are having problems casting from one data type to another specially from void to char or int.
First of all void* just means that the variable can pretty much hold any type of data, from character all the way unto float. Let me just show you >>
Lets say you want void *variable;
to be equal 100;
Now this is just going to give you a warning telling you
you did not cast it unto an integer (int).
In order to do that you will do it like this>>
Now lets work with character(char) >>
Now am going to show how to do it from a void function unto a int function>>
A function call var with an void parameter, Now you
may wonder how do i convert that to an integer(int) function.
Well its very easy >>
First i declare a new variable with data type of int
as pointer then set it equal to an integer function by casting it, then feeding that parameter inside var().
AGAIN!!! That was easy.
I use Dev-C++ 4.9.9.2 if you are having any problems
writing or debugging any program feel free to contact me i will gladly help you. =)
** Edit ** email removed
-JOEY
First of all void* just means that the variable can pretty much hold any type of data, from character all the way unto float. Let me just show you >>
Lets say you want void *variable;
to be equal 100;
Code:
void *variable = 100;
you did not cast it unto an integer (int).
In order to do that you will do it like this>>
Code:
void *variable =(int *) 100;
Code:
char *variable =(char *)"This is a message";
Now am going to show how to do it from a void function unto a int function>>
Code:
void *var(void *msg){printf("%s",msg);}
may wonder how do i convert that to an integer(int) function.
Well its very easy >>
Code:
char *function=(char *)var("hello world!\n");
as pointer then set it equal to an integer function by casting it, then feeding that parameter inside var().
AGAIN!!! That was easy.
I use Dev-C++ 4.9.9.2 if you are having any problems
writing or debugging any program feel free to contact me i will gladly help you. =)
** Edit ** email removed
-JOEY
Comment