How to convert const void* to unsigned int?












0















So in this code:



unsigned int lptr = lua_topointer(L, -1);


It gives the error:
cannot convert from 'const void *' to 'unsigned int'



I have tried reinterpret_cast to convert it like this:



unsigned int lptr = reinterpret_cast<const void*>(lua_topointer(L, -1)));


but it gives me another error that says this:
a value of type "const void*" cannot be used to initilize an entity of type "unsigned int"



any help would be appreciated.










share|improve this question


















  • 3





    Why do you want to do that? Are you trying to get the integer value of the address so that you can print it?

    – Nicol Bolas
    Nov 23 '18 at 2:17
















0















So in this code:



unsigned int lptr = lua_topointer(L, -1);


It gives the error:
cannot convert from 'const void *' to 'unsigned int'



I have tried reinterpret_cast to convert it like this:



unsigned int lptr = reinterpret_cast<const void*>(lua_topointer(L, -1)));


but it gives me another error that says this:
a value of type "const void*" cannot be used to initilize an entity of type "unsigned int"



any help would be appreciated.










share|improve this question


















  • 3





    Why do you want to do that? Are you trying to get the integer value of the address so that you can print it?

    – Nicol Bolas
    Nov 23 '18 at 2:17














0












0








0


0






So in this code:



unsigned int lptr = lua_topointer(L, -1);


It gives the error:
cannot convert from 'const void *' to 'unsigned int'



I have tried reinterpret_cast to convert it like this:



unsigned int lptr = reinterpret_cast<const void*>(lua_topointer(L, -1)));


but it gives me another error that says this:
a value of type "const void*" cannot be used to initilize an entity of type "unsigned int"



any help would be appreciated.










share|improve this question














So in this code:



unsigned int lptr = lua_topointer(L, -1);


It gives the error:
cannot convert from 'const void *' to 'unsigned int'



I have tried reinterpret_cast to convert it like this:



unsigned int lptr = reinterpret_cast<const void*>(lua_topointer(L, -1)));


but it gives me another error that says this:
a value of type "const void*" cannot be used to initilize an entity of type "unsigned int"



any help would be appreciated.







c++ lua






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 1:49









Sconley254Sconley254

12




12








  • 3





    Why do you want to do that? Are you trying to get the integer value of the address so that you can print it?

    – Nicol Bolas
    Nov 23 '18 at 2:17














  • 3





    Why do you want to do that? Are you trying to get the integer value of the address so that you can print it?

    – Nicol Bolas
    Nov 23 '18 at 2:17








3




3





Why do you want to do that? Are you trying to get the integer value of the address so that you can print it?

– Nicol Bolas
Nov 23 '18 at 2:17





Why do you want to do that? Are you trying to get the integer value of the address so that you can print it?

– Nicol Bolas
Nov 23 '18 at 2:17












2 Answers
2






active

oldest

votes


















3














lua_topointer returns a void const*. Your reinterpret_cast isn't actually changing anything. What you need to write is:



unsigned int value = reinterpret_cast<unsigned int>(lua_topointer(L, -1)));


But bear in mind that this will only work on platforms where the size of an int happens to match the size of a pointer. It's better to use std::uintptr_t from <cstdint> instead since the size is guaranteed to match.



std::uintptr_t value = reinterpret_cast<std::uintptr_t>(lua_topointer(L, -1)));





share|improve this answer

































    2














    It is unclear what you mean by converting a void pointer to an integer. If you intend to access the pointed object, then see the documentation of lua_topointer:




    Converts the value at the given acceptable index to a generic C pointer (void*). The value can be a userdata, a table, a thread, or a function; otherwise, lua_topointer returns NULL. Different objects will give different pointers. There is no way to convert the pointer back to its original value.



    Typically this function is used only for debug information.




    Also note that according to the quoted documentation, the pointed object cannot be an unsigned int.





    If you instead intend to represent the pointer as an integer, first I suggest that you consider why would you want to do that. It's not typically needed. Most reasonable things that can be done with the integer representation such as printing can be done with the pointer itself directly.



    But if you really want to "convert" a pointer to an integer, unsigned int is not a good choice since it is not guaranteed to be able to represent all values that can be represented by a data pointer.std::uintptr_t can represent all such values and this would be correct way to convert:



    reinterpret_cast<std::uintptr_t>(data_pointer)





    share|improve this answer





















    • 1





      reinterpret_cast of a pointer to an integral type of equal or greater size is also correct (en.cppreference.com/w/cpp/language/reinterpret_cast)

      – Peter Ruderman
      Nov 23 '18 at 2:22











    • @PeterRuderman good point. I shan't suggest using memcpy then.

      – eerorika
      Nov 23 '18 at 2:27













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439778%2fhow-to-convert-const-void-to-unsigned-int%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    lua_topointer returns a void const*. Your reinterpret_cast isn't actually changing anything. What you need to write is:



    unsigned int value = reinterpret_cast<unsigned int>(lua_topointer(L, -1)));


    But bear in mind that this will only work on platforms where the size of an int happens to match the size of a pointer. It's better to use std::uintptr_t from <cstdint> instead since the size is guaranteed to match.



    std::uintptr_t value = reinterpret_cast<std::uintptr_t>(lua_topointer(L, -1)));





    share|improve this answer






























      3














      lua_topointer returns a void const*. Your reinterpret_cast isn't actually changing anything. What you need to write is:



      unsigned int value = reinterpret_cast<unsigned int>(lua_topointer(L, -1)));


      But bear in mind that this will only work on platforms where the size of an int happens to match the size of a pointer. It's better to use std::uintptr_t from <cstdint> instead since the size is guaranteed to match.



      std::uintptr_t value = reinterpret_cast<std::uintptr_t>(lua_topointer(L, -1)));





      share|improve this answer




























        3












        3








        3







        lua_topointer returns a void const*. Your reinterpret_cast isn't actually changing anything. What you need to write is:



        unsigned int value = reinterpret_cast<unsigned int>(lua_topointer(L, -1)));


        But bear in mind that this will only work on platforms where the size of an int happens to match the size of a pointer. It's better to use std::uintptr_t from <cstdint> instead since the size is guaranteed to match.



        std::uintptr_t value = reinterpret_cast<std::uintptr_t>(lua_topointer(L, -1)));





        share|improve this answer















        lua_topointer returns a void const*. Your reinterpret_cast isn't actually changing anything. What you need to write is:



        unsigned int value = reinterpret_cast<unsigned int>(lua_topointer(L, -1)));


        But bear in mind that this will only work on platforms where the size of an int happens to match the size of a pointer. It's better to use std::uintptr_t from <cstdint> instead since the size is guaranteed to match.



        std::uintptr_t value = reinterpret_cast<std::uintptr_t>(lua_topointer(L, -1)));






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 2:15









        Nicol Bolas

        286k33475648




        286k33475648










        answered Nov 23 '18 at 1:56









        Peter RudermanPeter Ruderman

        10.2k2352




        10.2k2352

























            2














            It is unclear what you mean by converting a void pointer to an integer. If you intend to access the pointed object, then see the documentation of lua_topointer:




            Converts the value at the given acceptable index to a generic C pointer (void*). The value can be a userdata, a table, a thread, or a function; otherwise, lua_topointer returns NULL. Different objects will give different pointers. There is no way to convert the pointer back to its original value.



            Typically this function is used only for debug information.




            Also note that according to the quoted documentation, the pointed object cannot be an unsigned int.





            If you instead intend to represent the pointer as an integer, first I suggest that you consider why would you want to do that. It's not typically needed. Most reasonable things that can be done with the integer representation such as printing can be done with the pointer itself directly.



            But if you really want to "convert" a pointer to an integer, unsigned int is not a good choice since it is not guaranteed to be able to represent all values that can be represented by a data pointer.std::uintptr_t can represent all such values and this would be correct way to convert:



            reinterpret_cast<std::uintptr_t>(data_pointer)





            share|improve this answer





















            • 1





              reinterpret_cast of a pointer to an integral type of equal or greater size is also correct (en.cppreference.com/w/cpp/language/reinterpret_cast)

              – Peter Ruderman
              Nov 23 '18 at 2:22











            • @PeterRuderman good point. I shan't suggest using memcpy then.

              – eerorika
              Nov 23 '18 at 2:27


















            2














            It is unclear what you mean by converting a void pointer to an integer. If you intend to access the pointed object, then see the documentation of lua_topointer:




            Converts the value at the given acceptable index to a generic C pointer (void*). The value can be a userdata, a table, a thread, or a function; otherwise, lua_topointer returns NULL. Different objects will give different pointers. There is no way to convert the pointer back to its original value.



            Typically this function is used only for debug information.




            Also note that according to the quoted documentation, the pointed object cannot be an unsigned int.





            If you instead intend to represent the pointer as an integer, first I suggest that you consider why would you want to do that. It's not typically needed. Most reasonable things that can be done with the integer representation such as printing can be done with the pointer itself directly.



            But if you really want to "convert" a pointer to an integer, unsigned int is not a good choice since it is not guaranteed to be able to represent all values that can be represented by a data pointer.std::uintptr_t can represent all such values and this would be correct way to convert:



            reinterpret_cast<std::uintptr_t>(data_pointer)





            share|improve this answer





















            • 1





              reinterpret_cast of a pointer to an integral type of equal or greater size is also correct (en.cppreference.com/w/cpp/language/reinterpret_cast)

              – Peter Ruderman
              Nov 23 '18 at 2:22











            • @PeterRuderman good point. I shan't suggest using memcpy then.

              – eerorika
              Nov 23 '18 at 2:27
















            2












            2








            2







            It is unclear what you mean by converting a void pointer to an integer. If you intend to access the pointed object, then see the documentation of lua_topointer:




            Converts the value at the given acceptable index to a generic C pointer (void*). The value can be a userdata, a table, a thread, or a function; otherwise, lua_topointer returns NULL. Different objects will give different pointers. There is no way to convert the pointer back to its original value.



            Typically this function is used only for debug information.




            Also note that according to the quoted documentation, the pointed object cannot be an unsigned int.





            If you instead intend to represent the pointer as an integer, first I suggest that you consider why would you want to do that. It's not typically needed. Most reasonable things that can be done with the integer representation such as printing can be done with the pointer itself directly.



            But if you really want to "convert" a pointer to an integer, unsigned int is not a good choice since it is not guaranteed to be able to represent all values that can be represented by a data pointer.std::uintptr_t can represent all such values and this would be correct way to convert:



            reinterpret_cast<std::uintptr_t>(data_pointer)





            share|improve this answer















            It is unclear what you mean by converting a void pointer to an integer. If you intend to access the pointed object, then see the documentation of lua_topointer:




            Converts the value at the given acceptable index to a generic C pointer (void*). The value can be a userdata, a table, a thread, or a function; otherwise, lua_topointer returns NULL. Different objects will give different pointers. There is no way to convert the pointer back to its original value.



            Typically this function is used only for debug information.




            Also note that according to the quoted documentation, the pointed object cannot be an unsigned int.





            If you instead intend to represent the pointer as an integer, first I suggest that you consider why would you want to do that. It's not typically needed. Most reasonable things that can be done with the integer representation such as printing can be done with the pointer itself directly.



            But if you really want to "convert" a pointer to an integer, unsigned int is not a good choice since it is not guaranteed to be able to represent all values that can be represented by a data pointer.std::uintptr_t can represent all such values and this would be correct way to convert:



            reinterpret_cast<std::uintptr_t>(data_pointer)






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 23 '18 at 2:22

























            answered Nov 23 '18 at 2:05









            eerorikaeerorika

            81k559122




            81k559122








            • 1





              reinterpret_cast of a pointer to an integral type of equal or greater size is also correct (en.cppreference.com/w/cpp/language/reinterpret_cast)

              – Peter Ruderman
              Nov 23 '18 at 2:22











            • @PeterRuderman good point. I shan't suggest using memcpy then.

              – eerorika
              Nov 23 '18 at 2:27
















            • 1





              reinterpret_cast of a pointer to an integral type of equal or greater size is also correct (en.cppreference.com/w/cpp/language/reinterpret_cast)

              – Peter Ruderman
              Nov 23 '18 at 2:22











            • @PeterRuderman good point. I shan't suggest using memcpy then.

              – eerorika
              Nov 23 '18 at 2:27










            1




            1





            reinterpret_cast of a pointer to an integral type of equal or greater size is also correct (en.cppreference.com/w/cpp/language/reinterpret_cast)

            – Peter Ruderman
            Nov 23 '18 at 2:22





            reinterpret_cast of a pointer to an integral type of equal or greater size is also correct (en.cppreference.com/w/cpp/language/reinterpret_cast)

            – Peter Ruderman
            Nov 23 '18 at 2:22













            @PeterRuderman good point. I shan't suggest using memcpy then.

            – eerorika
            Nov 23 '18 at 2:27







            @PeterRuderman good point. I shan't suggest using memcpy then.

            – eerorika
            Nov 23 '18 at 2:27




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439778%2fhow-to-convert-const-void-to-unsigned-int%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin