Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How do we cast void type

How do we cast void type

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
20 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L luplup

    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;
    }

    J Offline
    J Offline
    jschell
    wrote on last edited by
    #3

    You are NOT using cast correctly. You must NOT cast types to different types. Just take that rule as an absolute until you have been programming in C/C++ for about 5 years. Besides other problems in your code you are taking a "short int" and treating it like an "int". That is wrong.

    L 1 Reply Last reply
    0
    • L Lost User

      Do not use void when you know the type that is to be operated on, it makes no sense. And your calc function would be better to return the calculated value, something like:

      int calc(int var) {
      //try convert short to int
      int tmp = var + 30003;

      return tmp;
      

      }

      I am not sure what the complexeShortToInt function, or the actual program, is supposed to do.

      L Offline
      L Offline
      luplup
      wrote on last edited by
      #4

      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
      #include

      void 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....

      L V 2 Replies Last reply
      0
      • J jschell

        You are NOT using cast correctly. You must NOT cast types to different types. Just take that rule as an absolute until you have been programming in C/C++ for about 5 years. Besides other problems in your code you are taking a "short int" and treating it like an "int". That is wrong.

        L Offline
        L Offline
        luplup
        wrote on last edited by
        #5

        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.

        V J 2 Replies Last reply
        0
        • L luplup

          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.

          V Offline
          V Offline
          Victor Nijegorodov
          wrote on last edited by
          #6

          luplup wrote:

          Do i have to use calloc/realloc for the int too ? like i used it for the char/struct array.

          Are you programming in C or C++? :confused:

          L 1 Reply Last reply
          0
          • V Victor Nijegorodov

            luplup wrote:

            Do i have to use calloc/realloc for the int too ? like i used it for the char/struct array.

            Are you programming in C or C++? :confused:

            L Offline
            L Offline
            luplup
            wrote on last edited by
            #7

            C

            L 1 Reply Last reply
            0
            • L luplup

              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;
              }

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #8

              1. Could you explain what are you trying to achieve using such type of very dangerous castings? 2.

              luplup wrote:

              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; }

              The problem with this "casting" is the origin of the parameter passed to is short integer (the low WORD in Win32 environment). But you are then trying to interpret it (somehow) as int causing to take into account the high WORD which contain garbage! So what do you expect from the next manipulations with this garbage? :zzz: Well "4 days" for such a trial & error method is more than enough to understand (what you should have done for a couple of hours if you properly used a debugger) that such a "simple" casting in a real C/C++ code is very very dangerous and should be avoided!

              L 1 Reply Last reply
              0
              • V Victor Nijegorodov

                1. Could you explain what are you trying to achieve using such type of very dangerous castings? 2.

                luplup wrote:

                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; }

                The problem with this "casting" is the origin of the parameter passed to is short integer (the low WORD in Win32 environment). But you are then trying to interpret it (somehow) as int causing to take into account the high WORD which contain garbage! So what do you expect from the next manipulations with this garbage? :zzz: Well "4 days" for such a trial & error method is more than enough to understand (what you should have done for a couple of hours if you properly used a debugger) that such a "simple" casting in a real C/C++ code is very very dangerous and should be avoided!

                L Offline
                L Offline
                luplup
                wrote on last edited by
                #9

                1. The explain is in this answer Re: How do we cast void type - C / C++ / MFC Discussion Boards[^] 2. is this a valid cast short to int ?

                int tmp = (short int)var;

                Edit: im already suspecting my &vInt2 because it is not pointer, like maybe the void* would have, im here to ask you the right way for using this void "type"...

                V 1 Reply Last reply
                0
                • L luplup

                  1. The explain is in this answer Re: How do we cast void type - C / C++ / MFC Discussion Boards[^] 2. is this a valid cast short to int ?

                  int tmp = (short int)var;

                  Edit: im already suspecting my &vInt2 because it is not pointer, like maybe the void* would have, im here to ask you the right way for using this void "type"...

                  V Offline
                  V Offline
                  Victor Nijegorodov
                  wrote on last edited by
                  #10

                  luplup wrote:

                  is this a valid cast short to int ?

                  int tmp = (short int)var;

                  Yes, if the var was initialized from/as some short int value.

                  1 Reply Last reply
                  0
                  • L luplup

                    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
                    #include

                    void 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....

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #11

                    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?
                    }

                    L 1 Reply Last reply
                    0
                    • L luplup

                      C

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      Well it doesn't matter whether it is C or C++, what you are trying to do is a waste of time because it will never work. Get hold of a good book on C programming and learn these things properly.

                      1 Reply Last reply
                      0
                      • L luplup

                        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
                        #include

                        void 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....

                        V Offline
                        V Offline
                        Victor Nijegorodov
                        wrote on last edited by
                        #13

                        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:

                        1 Reply Last reply
                        0
                        • L luplup

                          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;
                          }

                          V Offline
                          V Offline
                          Victor Nijegorodov
                          wrote on last edited by
                          #14

                          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) ?

                          1 Reply Last reply
                          0
                          • L Lost User

                            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?
                            }

                            L Offline
                            L Offline
                            luplup
                            wrote on last edited by
                            #15

                            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...

                            L 1 Reply Last reply
                            0
                            • L luplup

                              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...

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #16

                              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.

                              L 1 Reply Last reply
                              0
                              • L Lost User

                                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.

                                L Offline
                                L Offline
                                luplup
                                wrote on last edited by
                                #17

                                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!

                                J L 2 Replies Last reply
                                0
                                • L luplup

                                  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!

                                  J Offline
                                  J Offline
                                  jeron1
                                  wrote on last edited by
                                  #18

                                  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

                                  1 Reply Last reply
                                  0
                                  • L luplup

                                    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!

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #19

                                    luplup wrote:

                                    Never i will go back in this forum

                                    Well, there will be many a dry eye here when you leave.

                                    1 Reply Last reply
                                    0
                                    • L luplup

                                      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.

                                      J Offline
                                      J Offline
                                      jschell
                                      wrote on last edited by
                                      #20

                                      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.

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups