How do we cast void type
-
Surely it is not to put these shitty functions in my program, but it is to understand how i can handle this situation (so i reduced the code at max) And the real situation of calc is to substitute the standard + - * / operations with "handle" of the overflow. So for example a declared short int that excess the 32767 limit. I know the type at start, but maybe it will produce an overflow later, and maybe with difficulty to detect. So how we handle this situation !? Here another more complete code, that can represent a var++;
#include
#include
#include
#includevoid calc(char *type, void *var1, char calc, void *var2) {
int tmp1 = *(int*)var1;//bad
int tmp2 = *(int*)var2;//bad
/*//this "work" but i want these short converted to int
short int tmp1 = *(short int*)var1;
short int tmp2 = *(short int*)var2;*/
printf("tmp1: %i\n", tmp1);
printf("tmp2: %i\n", tmp2);int max, min;
unsigned int umax, umin;
short int overflow = 0;if(strncmp(type, "short int", 9)==0) {
max = SHRT_MAX;
min = SHRT_MIN;
}if(calc == '+') {
/*
if(overflow == 0) {
*(short int*)var1 = *(short int*)var1 + *(short int*)var2;
}
else{
//addition not in short range, so cast the original var
*(int*)var1 = tmp1 + tmp2;
}
*/
}
}int main() {
short int vInt2 = 32767;//i dont know what is his real value at the moment, maybe in a loop
short int vInt3 = 1;
calc("short int", &vInt2, '+', &vInt3);
printf("bad convert to int: %i\n", vInt2);
return 0;
}Edit: So if i dont use void, it seems i have to write lot of calc "identical" function for each type....
You misunderstand the problem I'm afraid. Using a void pointer makes no difference, as your pointers will still point to different types. For example:
void calc(void* pvoid)
{
int value = *(int*)pvoid:
// what happens if pvoid points to a floating point value?
} -
Surely it is not to put these shitty functions in my program, but it is to understand how i can handle this situation (so i reduced the code at max) And the real situation of calc is to substitute the standard + - * / operations with "handle" of the overflow. So for example a declared short int that excess the 32767 limit. I know the type at start, but maybe it will produce an overflow later, and maybe with difficulty to detect. So how we handle this situation !? Here another more complete code, that can represent a var++;
#include
#include
#include
#includevoid calc(char *type, void *var1, char calc, void *var2) {
int tmp1 = *(int*)var1;//bad
int tmp2 = *(int*)var2;//bad
/*//this "work" but i want these short converted to int
short int tmp1 = *(short int*)var1;
short int tmp2 = *(short int*)var2;*/
printf("tmp1: %i\n", tmp1);
printf("tmp2: %i\n", tmp2);int max, min;
unsigned int umax, umin;
short int overflow = 0;if(strncmp(type, "short int", 9)==0) {
max = SHRT_MAX;
min = SHRT_MIN;
}if(calc == '+') {
/*
if(overflow == 0) {
*(short int*)var1 = *(short int*)var1 + *(short int*)var2;
}
else{
//addition not in short range, so cast the original var
*(int*)var1 = tmp1 + tmp2;
}
*/
}
}int main() {
short int vInt2 = 32767;//i dont know what is his real value at the moment, maybe in a loop
short int vInt3 = 1;
calc("short int", &vInt2, '+', &vInt3);
printf("bad convert to int: %i\n", vInt2);
return 0;
}Edit: So if i dont use void, it seems i have to write lot of calc "identical" function for each type....
luplup wrote:
Edit: So if i dont use void, it seems i have to write lot of calc "identical" function for each type....
But you still do not explain the main reason you want to use this "void*" method implementation! :doh:
-
Hello, I got problem trying converting type when the var is declared void in function. How do you do these things !? for example casting short to int from void var (declared short at start) And can we change a variable type without having to create new var !? 4 days im on, and i dont get solution without rewriting 50 "identical" functions just with changed type... It's for that im looking to the void type. Please help :)
#include
void calc(void *var) {
//try convert short to int with new var
int tmp = *(int*)var;
printf("tmp: %i\n", tmp);//bad, must be 30002
//try cast/convert original var
*(int*)var += 30003;
}void complexeShortToInt() {
short int vInt2 = 30002;
calc(&vInt2);
printf("complexeShortToInt: %i\n", vInt2);//bad, must be 60005
}/*void simpleShortToInt() {
short int vInt = 30000;
int vOut = (int)vInt;
vOut += 30000;
printf("simpleShortToInt: %i\n", vOut);//good
}*/int main() {
//simpleShortToInt();//good
complexeShortToInt();//bad
return 0;
}Just a guess: are you looking for something like a [Variant Data Type](https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/variant-data-type) ?
-
You misunderstand the problem I'm afraid. Using a void pointer makes no difference, as your pointers will still point to different types. For example:
void calc(void* pvoid)
{
int value = *(int*)pvoid:
// what happens if pvoid points to a floating point value?
}You too you misunderstood my problem i think, what you say will be handle by the type="float", and so ok a little "repeat" in the function but i find this better than 15 calc functions for each type. So finally my real problem is for the "return value" that can have their type changed, its for that im thinking casting the void type that point to the initial variable, and i dont know if possible... im asking you. So my real problem is this &vInt2 declared short int at start, that i want can change his type inside the function calc(), and without to have creating a new variable in the main scope. Think that this vInt2 is a "main var" where i would do some operations, and automatically convert it to the right type if the initial type is not adapted. So can we change a variable type without creating a new var !? Finally if not possible, i can at least alert that this operation is bad for the initial type. But i wanted to change the type to a bigger if possible, and so i can start with short int and handle more and more if necessary... Edit: humm i spot a future problem that can break all when the type is changed so i would more think about how i can handle... Finally my first interrogation come from: how i can protect my program about overflow automatically So if you have more constructive suggestion...
-
You too you misunderstood my problem i think, what you say will be handle by the type="float", and so ok a little "repeat" in the function but i find this better than 15 calc functions for each type. So finally my real problem is for the "return value" that can have their type changed, its for that im thinking casting the void type that point to the initial variable, and i dont know if possible... im asking you. So my real problem is this &vInt2 declared short int at start, that i want can change his type inside the function calc(), and without to have creating a new variable in the main scope. Think that this vInt2 is a "main var" where i would do some operations, and automatically convert it to the right type if the initial type is not adapted. So can we change a variable type without creating a new var !? Finally if not possible, i can at least alert that this operation is bad for the initial type. But i wanted to change the type to a bigger if possible, and so i can start with short int and handle more and more if necessary... Edit: humm i spot a future problem that can break all when the type is changed so i would more think about how i can handle... Finally my first interrogation come from: how i can protect my program about overflow automatically So if you have more constructive suggestion...
-
luplup wrote:
You too you misunderstood my problem i think,
No, I understand completely; I am just saying that this is not the way to do it.
Ahah! finally got it... one var type changing, and one unique calc func, done WITH void* AND WITHOUT! Sorry but i will keep it for me, as the main rule in this forum... and in any event, its not useful for you, and you well know the way how to do that, its tabou, all MVP devs know its dangerous to try making program without undefined behavior, and the best is to start making app without thinking to that... so 5 years after, you can (eventually) begin thinking to it when the problem appear and maybe recode all from scratch, and the best for you, i think you will love it, it is you can resell another time your shitty program because it's not a bug, its feature in your process... Never i will go back in this forum, supposedly programming, the worst i ever seen... really you are not serious with your answers, you are trolling all the time on all the topics where you write something, but thank for me MacCutchan, you at least write me two lines of code, declaring a function and assigning variable, i dont know where i will be without your advanced experience... okay now i see from where come your 650 000 super value points... Whaou! you are really the best, without distinction! 9 times one of the best MVP helper... respect :) I was already having a little preview of what can be an MVP before coming here, and finally it can be worse than i was thinking... so please dont delete my messages, i will keep it for souvenirs, and to show the real things about what is an supposedly "MVP programmer/engineer" and his mentality too... however if you del and so have something to hide, you have to delete this entire forum too, almost completely full of useless answers! now i well understand why it is a desert here... Sorry for the possible real programmers that are shutting off all the time, but i dont know if there are some here.. no fact in the forum at least, and it seems all of you are "assisted" by fifty layers of abstraction/obscuration in your C++/MFC and then you think you are programmer... So at NEVER!
-
Ahah! finally got it... one var type changing, and one unique calc func, done WITH void* AND WITHOUT! Sorry but i will keep it for me, as the main rule in this forum... and in any event, its not useful for you, and you well know the way how to do that, its tabou, all MVP devs know its dangerous to try making program without undefined behavior, and the best is to start making app without thinking to that... so 5 years after, you can (eventually) begin thinking to it when the problem appear and maybe recode all from scratch, and the best for you, i think you will love it, it is you can resell another time your shitty program because it's not a bug, its feature in your process... Never i will go back in this forum, supposedly programming, the worst i ever seen... really you are not serious with your answers, you are trolling all the time on all the topics where you write something, but thank for me MacCutchan, you at least write me two lines of code, declaring a function and assigning variable, i dont know where i will be without your advanced experience... okay now i see from where come your 650 000 super value points... Whaou! you are really the best, without distinction! 9 times one of the best MVP helper... respect :) I was already having a little preview of what can be an MVP before coming here, and finally it can be worse than i was thinking... so please dont delete my messages, i will keep it for souvenirs, and to show the real things about what is an supposedly "MVP programmer/engineer" and his mentality too... however if you del and so have something to hide, you have to delete this entire forum too, almost completely full of useless answers! now i well understand why it is a desert here... Sorry for the possible real programmers that are shutting off all the time, but i dont know if there are some here.. no fact in the forum at least, and it seems all of you are "assisted" by fifty layers of abstraction/obscuration in your C++/MFC and then you think you are programmer... So at NEVER!
luplup wrote:
Never i will go back in this forum
Let's hope so.:thumbsup:
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
Ahah! finally got it... one var type changing, and one unique calc func, done WITH void* AND WITHOUT! Sorry but i will keep it for me, as the main rule in this forum... and in any event, its not useful for you, and you well know the way how to do that, its tabou, all MVP devs know its dangerous to try making program without undefined behavior, and the best is to start making app without thinking to that... so 5 years after, you can (eventually) begin thinking to it when the problem appear and maybe recode all from scratch, and the best for you, i think you will love it, it is you can resell another time your shitty program because it's not a bug, its feature in your process... Never i will go back in this forum, supposedly programming, the worst i ever seen... really you are not serious with your answers, you are trolling all the time on all the topics where you write something, but thank for me MacCutchan, you at least write me two lines of code, declaring a function and assigning variable, i dont know where i will be without your advanced experience... okay now i see from where come your 650 000 super value points... Whaou! you are really the best, without distinction! 9 times one of the best MVP helper... respect :) I was already having a little preview of what can be an MVP before coming here, and finally it can be worse than i was thinking... so please dont delete my messages, i will keep it for souvenirs, and to show the real things about what is an supposedly "MVP programmer/engineer" and his mentality too... however if you del and so have something to hide, you have to delete this entire forum too, almost completely full of useless answers! now i well understand why it is a desert here... Sorry for the possible real programmers that are shutting off all the time, but i dont know if there are some here.. no fact in the forum at least, and it seems all of you are "assisted" by fifty layers of abstraction/obscuration in your C++/MFC and then you think you are programmer... So at NEVER!
-
So please can you show me the right way to cast correctly :) I dont want to "lose" my time during 5 years before start learning "real" programming. So PLEASE show me a tiny code in the right direction, and i will pass 5 years studing it. Do i have to use calloc/realloc for the int too ? like i used it for the char/struct array.
luplup wrote:
So please can you show me the right way to cast correctly
That would require a book. You do it by understanding the data types that the pointer points to. What they represent, how they are represented in memory and what memory means in C (and computers in general) especially in relationship to the heap and the stack. And being very careful. If you wish to learn in detail then write your own printf() function and test completely.