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 to convert a pointer to char*?

How to convert a pointer to char*?

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialperformancequestion
6 Posts 6 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.
  • D Offline
    D Offline
    DaFrawg
    wrote on last edited by
    #1

    (I know this isn't really VC++)(Or is it?)(Anyway, it's not MFC) Example: void main(){ int nInt; int *pInt; cout << /*help?!*/ } I want to output the memory location of nInt. This location is saved into pInt :sigh:. The memory location is for instance 3031:3233, so pInt contains 0x30313233. If I would convert this (how) to char*, it'd say "0123". But what I want is 0x30313233 to be converted to a string saying "0x30313233". How is that possible? :confused: I know it is weird, but this is what I tried: void main(){ int nInt; int *pInt; cout << char*(void*(pInt)); } //Excuse me for my bad English. :-O //---QUOTE--- // //"ERROR: Keyboard not present - Press F1 to continue" - Most BOIS chips

    B A I D J 5 Replies Last reply
    0
    • D DaFrawg

      (I know this isn't really VC++)(Or is it?)(Anyway, it's not MFC) Example: void main(){ int nInt; int *pInt; cout << /*help?!*/ } I want to output the memory location of nInt. This location is saved into pInt :sigh:. The memory location is for instance 3031:3233, so pInt contains 0x30313233. If I would convert this (how) to char*, it'd say "0123". But what I want is 0x30313233 to be converted to a string saying "0x30313233". How is that possible? :confused: I know it is weird, but this is what I tried: void main(){ int nInt; int *pInt; cout << char*(void*(pInt)); } //Excuse me for my bad English. :-O //---QUOTE--- // //"ERROR: Keyboard not present - Press F1 to continue" - Most BOIS chips

      B Offline
      B Offline
      Bernhard
      wrote on last edited by
      #2

      Maybe this works? (haven't tried it out yet) cout << "hex = 0x" << hex << (void*) pInt;


      "I'm from the South Bronx, and I don't care what you say: those cows look dangerous."
      U.S. Secretary of State Colin Powell at George Bush's ranch in Texas

      1 Reply Last reply
      0
      • D DaFrawg

        (I know this isn't really VC++)(Or is it?)(Anyway, it's not MFC) Example: void main(){ int nInt; int *pInt; cout << /*help?!*/ } I want to output the memory location of nInt. This location is saved into pInt :sigh:. The memory location is for instance 3031:3233, so pInt contains 0x30313233. If I would convert this (how) to char*, it'd say "0123". But what I want is 0x30313233 to be converted to a string saying "0x30313233". How is that possible? :confused: I know it is weird, but this is what I tried: void main(){ int nInt; int *pInt; cout << char*(void*(pInt)); } //Excuse me for my bad English. :-O //---QUOTE--- // //"ERROR: Keyboard not present - Press F1 to continue" - Most BOIS chips

        A Offline
        A Offline
        Adam Gritt
        wrote on last edited by
        #3

        take a look at the function sprintf. it has the features that you are looking for and is part of the c runtime.

        1 Reply Last reply
        0
        • D DaFrawg

          (I know this isn't really VC++)(Or is it?)(Anyway, it's not MFC) Example: void main(){ int nInt; int *pInt; cout << /*help?!*/ } I want to output the memory location of nInt. This location is saved into pInt :sigh:. The memory location is for instance 3031:3233, so pInt contains 0x30313233. If I would convert this (how) to char*, it'd say "0123". But what I want is 0x30313233 to be converted to a string saying "0x30313233". How is that possible? :confused: I know it is weird, but this is what I tried: void main(){ int nInt; int *pInt; cout << char*(void*(pInt)); } //Excuse me for my bad English. :-O //---QUOTE--- // //"ERROR: Keyboard not present - Press F1 to continue" - Most BOIS chips

          I Offline
          I Offline
          Iain Clarke Warrior Programmer
          wrote on last edited by
          #4

          int nInt = 54321;
          int *pInt = &nInt;
          char buf [11]; // 8(number) + 2(0x) + 1(trailing null)
          sprintf (buf, "0x%08x", (unsigned int)(pInt));
          cout << buf;

          I must be in a good mood. Normally I'm not this helpful! Iain.

          1 Reply Last reply
          0
          • D DaFrawg

            (I know this isn't really VC++)(Or is it?)(Anyway, it's not MFC) Example: void main(){ int nInt; int *pInt; cout << /*help?!*/ } I want to output the memory location of nInt. This location is saved into pInt :sigh:. The memory location is for instance 3031:3233, so pInt contains 0x30313233. If I would convert this (how) to char*, it'd say "0123". But what I want is 0x30313233 to be converted to a string saying "0x30313233". How is that possible? :confused: I know it is weird, but this is what I tried: void main(){ int nInt; int *pInt; cout << char*(void*(pInt)); } //Excuse me for my bad English. :-O //---QUOTE--- // //"ERROR: Keyboard not present - Press F1 to continue" - Most BOIS chips

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            DaFrawg wrote: I want to output the memory location of nInt. This printed out the address for me:

            int nNumber,
            *pAddr;

            pAddr = &nNumber;
            cout << pAddr << endl;

            If you want that address in a char* variable, use:

            char addr[32];
            sprintf(addr, "%#x", pAddr);
            cout << addr << endl;


            Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

            1 Reply Last reply
            0
            • D DaFrawg

              (I know this isn't really VC++)(Or is it?)(Anyway, it's not MFC) Example: void main(){ int nInt; int *pInt; cout << /*help?!*/ } I want to output the memory location of nInt. This location is saved into pInt :sigh:. The memory location is for instance 3031:3233, so pInt contains 0x30313233. If I would convert this (how) to char*, it'd say "0123". But what I want is 0x30313233 to be converted to a string saying "0x30313233". How is that possible? :confused: I know it is weird, but this is what I tried: void main(){ int nInt; int *pInt; cout << char*(void*(pInt)); } //Excuse me for my bad English. :-O //---QUOTE--- // //"ERROR: Keyboard not present - Press F1 to continue" - Most BOIS chips

              J Offline
              J Offline
              JT Anderson
              wrote on last edited by
              #6

              How about this: #include void main() { int iVar; void *pVar = &iVar; std::cout << pVar << std::endl; } -------- There are 10 types of people in this world. Those who know binary and those who don't.

              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