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 use memset to fill a char array with space key?

how to use memset to fill a char array with space key?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
8 Posts 4 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.
  • F Offline
    F Offline
    focusdoit
    wrote on last edited by
    #1

    Hi, I want to fill out a char array with space. like:

    memset(msgArray, '*', 100);

    but '*' should be a Space key.

    L L 2 Replies Last reply
    0
    • F focusdoit

      Hi, I want to fill out a char array with space. like:

      memset(msgArray, '*', 100);

      but '*' should be a Space key.

      L Offline
      L Offline
      leon de boer
      wrote on last edited by
      #2

      memset(msgArray, ' ', 100); memset(msgArray, #32, 100); or a myriad of others

      In vino veritas

      F 1 Reply Last reply
      0
      • L leon de boer

        memset(msgArray, ' ', 100); memset(msgArray, #32, 100); or a myriad of others

        In vino veritas

        F Offline
        F Offline
        focusdoit
        wrote on last edited by
        #3

        Thanks, ' ' and 32 work. in fact, it's snprintf() reason. snprintf seems fill out the gap in a string with '\0'.

        1 Reply Last reply
        0
        • F focusdoit

          Hi, I want to fill out a char array with space. like:

          memset(msgArray, '*', 100);

          but '*' should be a Space key.

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

          I find this question a bit mysterious, if you had tried the obvious thing it just should have worked. The "obvious thing" is:

          focusdoit wrote:

          '*' should be a Space key.

          .. literally just replace '*' by ' '.

          F 1 Reply Last reply
          0
          • L Lost User

            I find this question a bit mysterious, if you had tried the obvious thing it just should have worked. The "obvious thing" is:

            focusdoit wrote:

            '*' should be a Space key.

            .. literally just replace '*' by ' '.

            F Offline
            F Offline
            focusdoit
            wrote on last edited by
            #5

            in fact, it's snprintf() reason. snprintf seems fill out the gap in a string with '\0'. the code like:

            memset (msgArray, ' ', 100);
            snprintf (msgArray, 2, "%d", 1);
            snprintf (messageArray+10, 33, "%s", "main");

            I assume the string should output like: 1 main but debugged it. it is : 1,\0, ' ', ' ', .. main, \0, ..

            L R L 3 Replies Last reply
            0
            • F focusdoit

              in fact, it's snprintf() reason. snprintf seems fill out the gap in a string with '\0'. the code like:

              memset (msgArray, ' ', 100);
              snprintf (msgArray, 2, "%d", 1);
              snprintf (messageArray+10, 33, "%s", "main");

              I assume the string should output like: 1 main but debugged it. it is : 1,\0, ' ', ' ', .. main, \0, ..

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

              Well snprintf terminates its output with a \0, which is typically useful (it would be a bit dangerous if it didn't), you can just overwrite that \0 with a space.

              1 Reply Last reply
              0
              • F focusdoit

                in fact, it's snprintf() reason. snprintf seems fill out the gap in a string with '\0'. the code like:

                memset (msgArray, ' ', 100);
                snprintf (msgArray, 2, "%d", 1);
                snprintf (messageArray+10, 33, "%s", "main");

                I assume the string should output like: 1 main but debugged it. it is : 1,\0, ' ', ' ', .. main, \0, ..

                R Offline
                R Offline
                Rick York
                wrote on last edited by
                #7

                Here's the prototype for the function :

                int _snprintf( char *buffer, size_t count, const char *format, ... );

                I usually use it like this :

                const size_t bufferSize = 127
                char buffer[bufferSize+1] = {0};

                _snprintf( buffer, bufferSize, "%3d %s", index, yourString );

                You can combine the _snprintf calls into one since they are going into the same memory buffer.

                1 Reply Last reply
                0
                • F focusdoit

                  in fact, it's snprintf() reason. snprintf seems fill out the gap in a string with '\0'. the code like:

                  memset (msgArray, ' ', 100);
                  snprintf (msgArray, 2, "%d", 1);
                  snprintf (messageArray+10, 33, "%s", "main");

                  I assume the string should output like: 1 main but debugged it. it is : 1,\0, ' ', ' ', .. main, \0, ..

                  L Offline
                  L Offline
                  leon de boer
                  wrote on last edited by
                  #8

                  That is because you are doing it all wrong you either join them all up in one sequence or move the first pointer along They are designed to be used a completely different way :-) Single line:

                  snprintf (msgArray, sizeof(msgArray),"%d%s", 1,"main");

                  Concat sequence (with buffer safety):

                  char *cur = &msgArray[0];
                  char *end = &msgArray[sizeof(msgArray)-1];
                  cur += snprintf(cur, end-cur, "%d", 1);
                  snprintf(cur, end-cur, "%s", "main");

                  In vino veritas

                  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