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. String from pointer to a char?

String from pointer to a char?

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsharpc++tutorial
3 Posts 3 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.
  • O Offline
    O Offline
    o m n i
    wrote on last edited by
    #1

    This is gonna sound like a really stupid question, but I'm new to C++ (been using C#). If I have a pointer to a char, how can I display the char in a messagebox? (I know to use MessageBoxA but I'm not sure how to make the char into something the MessageBoxA function accepts)

    D C 2 Replies Last reply
    0
    • O o m n i

      This is gonna sound like a really stupid question, but I'm new to C++ (been using C#). If I have a pointer to a char, how can I display the char in a messagebox? (I know to use MessageBoxA but I'm not sure how to make the char into something the MessageBoxA function accepts)

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

      Omnicoder wrote:

      If I have a pointer to a char...

      Which looks like:

      char c;
      char *p = &c;

      Omnicoder wrote:

      how can I display the char in a messagebox?

      You'll need a pointer to an "array" of characters, like:

      char *p = "Hello World";
      MessageBox(p);

      MessageBox() expects a pointer to a null-terminated string which is different than a pointer to a character.

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      1 Reply Last reply
      0
      • O o m n i

        This is gonna sound like a really stupid question, but I'm new to C++ (been using C#). If I have a pointer to a char, how can I display the char in a messagebox? (I know to use MessageBoxA but I'm not sure how to make the char into something the MessageBoxA function accepts)

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        In addition to David's answer, I want to say that you should never call a specific version of a function like MessageBox (so, calling MEssageBoxA or MessageBoxW is wrong). You should always stick to the generic version (without the A or W at the end). Character encoding causes in general a lot of troubles to new programmers, so I strongly suggest this excellent article[^] in order to better understand the concepts. Even if your code compiles now, reading this article can save you a LOT of time for the future (and probably a lot of problems too :-) ).

        Cédric Moonen Software developer
        Charting control [v2.0] OpenGL game tutorial in C++

        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