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. char pointer question

char pointer question

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
10 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.
  • Y Offline
    Y Offline
    Yadrif
    wrote on last edited by
    #1

    Hi All, I was playing around with char pointers just trying to learn things and noticed that if I malloc the size of a char and set what the returned poitner points at to be 'AB', when I cout what the pointer points to it writes out 'B'. This is the code: char *mainBuf = NULL; mainBuf = (char*)malloc(sizeof(char)); *mainBuf = 'AB'; cout << "mainBuf=" << mainBuf << endl; The output is: mainBuf=B I really didn't know what this would do as like I said I was experimenting. In a way I was surprised it did not crash because I thought cout looked for the terminating null character. Can anyone help me understand why this does what it does? Thanks.

    D 1 Reply Last reply
    0
    • Y Yadrif

      Hi All, I was playing around with char pointers just trying to learn things and noticed that if I malloc the size of a char and set what the returned poitner points at to be 'AB', when I cout what the pointer points to it writes out 'B'. This is the code: char *mainBuf = NULL; mainBuf = (char*)malloc(sizeof(char)); *mainBuf = 'AB'; cout << "mainBuf=" << mainBuf << endl; The output is: mainBuf=B I really didn't know what this would do as like I said I was experimenting. In a way I was surprised it did not crash because I thought cout looked for the terminating null character. Can anyone help me understand why this does what it does? Thanks.

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

      yadrif wrote:

      mainBuf = (char*)malloc(sizeof(char)); *mainBuf = 'AB';

      What exactly are you expecting to do with this? :confused: You are passing 1 to malloc(), yet you are trying to stuff, albeit incorrectly, 2 bytes into mainBuf. Why? Why are you using malloc() with a C++ program. Use new instead:

      char *mainBuf = new char[3];
      strcpy(mainBuf, "AB");
      delete [] mainBuf;


      "A good athlete is the result of a good and worthy opponent." - David Crow

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      C Y 2 Replies Last reply
      0
      • D David Crow

        yadrif wrote:

        mainBuf = (char*)malloc(sizeof(char)); *mainBuf = 'AB';

        What exactly are you expecting to do with this? :confused: You are passing 1 to malloc(), yet you are trying to stuff, albeit incorrectly, 2 bytes into mainBuf. Why? Why are you using malloc() with a C++ program. Use new instead:

        char *mainBuf = new char[3];
        strcpy(mainBuf, "AB");
        delete [] mainBuf;


        "A good athlete is the result of a good and worthy opponent." - David Crow

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

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

        DavidCrow wrote:

        yet you are trying to stuff, albeit incorrectly, 2 bytes into mainBuf

        Nope, 'AB' is one byte (it is different than "AB"), this will be truncated to one byte. But I think the compiler should give an error there instead of a warning...


        Cédric Moonen Software developer
        Charting control [v1.2]

        D M 2 Replies Last reply
        0
        • C Cedric Moonen

          DavidCrow wrote:

          yet you are trying to stuff, albeit incorrectly, 2 bytes into mainBuf

          Nope, 'AB' is one byte (it is different than "AB"), this will be truncated to one byte. But I think the compiler should give an error there instead of a warning...


          Cédric Moonen Software developer
          Charting control [v1.2]

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

          Point taken, but it's wrong nonetheless.


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          1 Reply Last reply
          0
          • D David Crow

            yadrif wrote:

            mainBuf = (char*)malloc(sizeof(char)); *mainBuf = 'AB';

            What exactly are you expecting to do with this? :confused: You are passing 1 to malloc(), yet you are trying to stuff, albeit incorrectly, 2 bytes into mainBuf. Why? Why are you using malloc() with a C++ program. Use new instead:

            char *mainBuf = new char[3];
            strcpy(mainBuf, "AB");
            delete [] mainBuf;


            "A good athlete is the result of a good and worthy opponent." - David Crow

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            Y Offline
            Y Offline
            Yadrif
            wrote on last edited by
            #5

            I was just playing around with the code, no real application here. Just learning. I actually started by writing a sample to use a pointer to a pointer. Just to learn how it worked and actually use what I read. I then for some reason decided to stuff 'AB' into what the pointer points to just to see what would happen. I then removed the pointer to pointer part to simplify it before posting the question. Maybe this just gets into the realm of undefined behavior. I thought perhaps there would be an explanation as to why it does what it does. Thanks.

            D 1 Reply Last reply
            0
            • C Cedric Moonen

              DavidCrow wrote:

              yet you are trying to stuff, albeit incorrectly, 2 bytes into mainBuf

              Nope, 'AB' is one byte (it is different than "AB"), this will be truncated to one byte. But I think the compiler should give an error there instead of a warning...


              Cédric Moonen Software developer
              Charting control [v1.2]

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              Nope, 'AB' is a short int that will truncated to one byte ;P

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              C 1 Reply Last reply
              0
              • Y Yadrif

                I was just playing around with the code, no real application here. Just learning. I actually started by writing a sample to use a pointer to a pointer. Just to learn how it worked and actually use what I read. I then for some reason decided to stuff 'AB' into what the pointer points to just to see what would happen. I then removed the pointer to pointer part to simplify it before posting the question. Maybe this just gets into the realm of undefined behavior. I thought perhaps there would be an explanation as to why it does what it does. Thanks.

                D Offline
                D Offline
                DQNOK
                wrote on last edited by
                #7

                I'm sorry no one here has taken the effort to give you a good reply. I also would like to know the answer. I've never used syntax like 'AB' since I've always dealt with ASCII strings, and the single 'apostophe' returns the character (integer) value of a single character between the marks. I don't know what it does in your context, but appears that perhaps it's playing "little endian" and returning the least-significant byte of a 16-bit value??? Did you try declaring the pointer as a wchar_t* instead? This might work. Maybe some real coders will see your question in a while and respond with a real answer. David

                R 1 Reply Last reply
                0
                • M Mark Salsbery

                  Nope, 'AB' is a short int that will truncated to one byte ;P

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

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

                  :rolleyes:


                  Cédric Moonen Software developer
                  Charting control [v1.2]

                  M 1 Reply Last reply
                  0
                  • C Cedric Moonen

                    :rolleyes:


                    Cédric Moonen Software developer
                    Charting control [v1.2]

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #9

                    :-D

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    1 Reply Last reply
                    0
                    • D DQNOK

                      I'm sorry no one here has taken the effort to give you a good reply. I also would like to know the answer. I've never used syntax like 'AB' since I've always dealt with ASCII strings, and the single 'apostophe' returns the character (integer) value of a single character between the marks. I don't know what it does in your context, but appears that perhaps it's playing "little endian" and returning the least-significant byte of a 16-bit value??? Did you try declaring the pointer as a wchar_t* instead? This might work. Maybe some real coders will see your question in a while and respond with a real answer. David

                      R Offline
                      R Offline
                      Robert Surtees
                      wrote on last edited by
                      #10

                      DQNOK wrote:

                      but appears that perhaps it's playing "little endian" and returning the least-significant byte of a 16-bit value???

                      This is correct.

                      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