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. array of char-newbie

array of char-newbie

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
21 Posts 9 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.
  • A Offline
    A Offline
    antonaras
    wrote on last edited by
    #1

    Hi again guys i have a stupit question lets say i have an array of char char string[50]; i put some characters in it and then i want to erase the content of the array so i can put some other characters in it so anyone knows how i erase all the values from the array? thanks a lot

    V J T Z 4 Replies Last reply
    0
    • A antonaras

      Hi again guys i have a stupit question lets say i have an array of char char string[50]; i put some characters in it and then i want to erase the content of the array so i can put some other characters in it so anyone knows how i erase all the values from the array? thanks a lot

      V Offline
      V Offline
      Viorel
      wrote on last edited by
      #2

      If you put characters into array using strcpy function, the old characters will be overwritten by new string; therefore there is no need to erase the old content. If you want to erase the old content in order to wipe some important information, for instance the password, you can use memset(string, 0, sizeof(string)).

      1 Reply Last reply
      0
      • A antonaras

        Hi again guys i have a stupit question lets say i have an array of char char string[50]; i put some characters in it and then i want to erase the content of the array so i can put some other characters in it so anyone knows how i erase all the values from the array? thanks a lot

        J Offline
        J Offline
        Justin Tay
        wrote on last edited by
        #3

        If what you want to have is just a null terminated C string. There's simply no need to erase the values of the array. The trailing 0 will be the thing signalling the end of the string. You must be dead sure that your string will never be more than 49 chars long. If not just use one of the string classes.

        1 Reply Last reply
        0
        • A antonaras

          Hi again guys i have a stupit question lets say i have an array of char char string[50]; i put some characters in it and then i want to erase the content of the array so i can put some other characters in it so anyone knows how i erase all the values from the array? thanks a lot

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          simply write your new string in your array... overwritting the buffer will do.


          TOXCCT >>> GEII power

          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

          A 1 Reply Last reply
          0
          • A antonaras

            Hi again guys i have a stupit question lets say i have an array of char char string[50]; i put some characters in it and then i want to erase the content of the array so i can put some other characters in it so anyone knows how i erase all the values from the array? thanks a lot

            Z Offline
            Z Offline
            Zac Howland
            wrote on last edited by
            #5

            // create a new character buffer initialized to 0's const unsigned int MAX_S_SIZE = 50; char s[MAX_S_SIZE] = {0}; // put some stuff in the buffer strcpy(s, "Hello, world!"); // clear the buffer memset(s, 0, MAX_S_SIZE); // NOTE: for an easier transition into UNICODE, the following works better const unsigned int MAX_S_SIZE = 50; TCHAR s[MAX_S_SIZE] = {0}; _tscpy(s, _T("Hello, World!")); memset(s, 0, MAX_S_SIZE * sizeof(TCHAR)); As a side note: you don't need to clear a buffer to overwrite it. It is more efficient to just overwrite it with the new data if your requirements allow for it. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

            A N 2 Replies Last reply
            0
            • T toxcct

              simply write your new string in your array... overwritting the buffer will do.


              TOXCCT >>> GEII power

              [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

              A Offline
              A Offline
              antonaras
              wrote on last edited by
              #6

              thanks toxcct but if i overwrite and the new sequence of characters is smaller than the one before some values will still be in the array for example if the the first time i insert 30 characters and the second time only 10 i ll have a problem yes? thanks again

              T D 2 Replies Last reply
              0
              • Z Zac Howland

                // create a new character buffer initialized to 0's const unsigned int MAX_S_SIZE = 50; char s[MAX_S_SIZE] = {0}; // put some stuff in the buffer strcpy(s, "Hello, world!"); // clear the buffer memset(s, 0, MAX_S_SIZE); // NOTE: for an easier transition into UNICODE, the following works better const unsigned int MAX_S_SIZE = 50; TCHAR s[MAX_S_SIZE] = {0}; _tscpy(s, _T("Hello, World!")); memset(s, 0, MAX_S_SIZE * sizeof(TCHAR)); As a side note: you don't need to clear a buffer to overwrite it. It is more efficient to just overwrite it with the new data if your requirements allow for it. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                A Offline
                A Offline
                antonaras
                wrote on last edited by
                #7

                you said::As a side note: you don't need to clear a buffer to overwrite it. It is more efficient to just overwrite it with the new data if your requirements allow for it. but if i overwrite and the new sequence of characters is smaller than the one before some values will still be in the array for example if the the first time i insert 30 characters and the second time only 10 i ll have a problem yes? Thanks for the help Zac Howland

                Z J 2 Replies Last reply
                0
                • A antonaras

                  thanks toxcct but if i overwrite and the new sequence of characters is smaller than the one before some values will still be in the array for example if the the first time i insert 30 characters and the second time only 10 i ll have a problem yes? thanks again

                  T Offline
                  T Offline
                  toxcct
                  wrote on last edited by
                  #8

                  no, because C-Style strings are NULL terminated ; that means that the last character in the string is the character '\0', so that when this character is encountered, the reader knows he reached the end of the string (whatever bytes remaining in the trailing space of the buffer)...


                  TOXCCT >>> GEII power

                  [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                  1 Reply Last reply
                  0
                  • A antonaras

                    you said::As a side note: you don't need to clear a buffer to overwrite it. It is more efficient to just overwrite it with the new data if your requirements allow for it. but if i overwrite and the new sequence of characters is smaller than the one before some values will still be in the array for example if the the first time i insert 30 characters and the second time only 10 i ll have a problem yes? Thanks for the help Zac Howland

                    Z Offline
                    Z Offline
                    Zac Howland
                    wrote on last edited by
                    #9

                    If you are reading in character buffers as a whole, yes, you will have a problem. If you are using them as C-style strings, then no, you won't. The reasoning is that C-style strings have a null-terminator. Example: const unsigned int ARRAY_SIZE = 20; char myBuffer[ARRAY_SIZE] = {0}; // Right now, the entire buffer is 0's strcpy(myBuffer, "My funny String"); cout << myBuffer << endl; cout << strlen(myBuffer) << endl; // Output will be: // My funny String // 15 // Now, replace the buffer strcpy(myBuffer, "Hello"); cout << myBuffer << endl; cout << strlen(myBuffer) << endl; // Output will be: // Hello // 5 The only time you would run into problems doing this is when you will be looking at the entire character buffer (so all 20 characters) for information. However, in that case, you most likely will not be treating the buffer as a string, and will not be using the strXXX family of functions on it either (or their _tsc equivalents). If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                    1 Reply Last reply
                    0
                    • A antonaras

                      you said::As a side note: you don't need to clear a buffer to overwrite it. It is more efficient to just overwrite it with the new data if your requirements allow for it. but if i overwrite and the new sequence of characters is smaller than the one before some values will still be in the array for example if the the first time i insert 30 characters and the second time only 10 i ll have a problem yes? Thanks for the help Zac Howland

                      J Offline
                      J Offline
                      James R Twine
                      wrote on last edited by
                      #10

                      The standard string-handling functions like strcpy(...), strcat(...), sprintf(...), etc. will write a terminating NUL (not a NULL[^]) into the buffer you are using.    So if you use them, you normally do not have to worry about previous data causing issues (unless you are concerned about security, but that is another issue).  Here is an example:

                      char caBuffer[ 50 ];

                      strcpy( caBuffer, "A Looooooooong String" );
                      strcpy( caBuffer, "A Short String" );
                      puts( caBuffer );

                      After the first call to strcpy(..) the buffer will contain (NUL is represented by Ø):

                      0123456789012345678901
                      

                      -and after the second call to strcpy(...), the buffer will likely contain:

                      0123456789012345678901
                      

                      When the string is shown using puts(...), it will display A Short String, even though the actual memory for the buffer has the extra data in it.  Since string-handling functions use NUL as an end-of-string indicator, puts(...) stops when it gets to the first NUL it encounters.    Peace! -=- James


                      If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                      Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                      DeleteFXPFiles & CheckFavorites (Please rate this post!)

                      J 1 Reply Last reply
                      0
                      • A antonaras

                        thanks toxcct but if i overwrite and the new sequence of characters is smaller than the one before some values will still be in the array for example if the the first time i insert 30 characters and the second time only 10 i ll have a problem yes? thanks again

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

                        antonaras wrote:

                        i ll have a problem yes?

                        Only if the presence of characters 10-29 are an issue. If you are dealing with nul-terminated strings, the string-related functions (e.g., strcpy(), strlen()) will terminate at the first \0 character. Otherwise, no issue.


                        "The largest fire starts but with the smallest spark." - David Crow

                        "Judge not by the eye but by the heart." - Native American Proverb

                        1 Reply Last reply
                        0
                        • Z Zac Howland

                          // create a new character buffer initialized to 0's const unsigned int MAX_S_SIZE = 50; char s[MAX_S_SIZE] = {0}; // put some stuff in the buffer strcpy(s, "Hello, world!"); // clear the buffer memset(s, 0, MAX_S_SIZE); // NOTE: for an easier transition into UNICODE, the following works better const unsigned int MAX_S_SIZE = 50; TCHAR s[MAX_S_SIZE] = {0}; _tscpy(s, _T("Hello, World!")); memset(s, 0, MAX_S_SIZE * sizeof(TCHAR)); As a side note: you don't need to clear a buffer to overwrite it. It is more efficient to just overwrite it with the new data if your requirements allow for it. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                          N Offline
                          N Offline
                          Nish Nishant
                          wrote on last edited by
                          #12

                          Why is Zac's answer low-voted? It was an accurate answer, wasn't it? :confused: Regards, Nish


                          Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                          Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

                          Z A J 3 Replies Last reply
                          0
                          • N Nish Nishant

                            Why is Zac's answer low-voted? It was an accurate answer, wasn't it? :confused: Regards, Nish


                            Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                            Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

                            Z Offline
                            Z Offline
                            Zac Howland
                            wrote on last edited by
                            #13

                            Nishant Sivakumar wrote:

                            Why is Zac's answer low-voted? It was an accurate answer, wasn't it?

                            Yes, it is accurate. Some people get hung up on the terminology (actually, just the spelling) of NUL vs NULL. In my opinion, as long as you know that you are talking about putting a 0 somewhere, it doesn't matter whether you call it a "nul-terminator" or a "null-terminator". If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                            J 1 Reply Last reply
                            0
                            • N Nish Nishant

                              Why is Zac's answer low-voted? It was an accurate answer, wasn't it? :confused: Regards, Nish


                              Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                              Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

                              A Offline
                              A Offline
                              antonaras
                              wrote on last edited by
                              #14

                              Personally i found the answer of Zac very accurate and helpful

                              1 Reply Last reply
                              0
                              • Z Zac Howland

                                Nishant Sivakumar wrote:

                                Why is Zac's answer low-voted? It was an accurate answer, wasn't it?

                                Yes, it is accurate. Some people get hung up on the terminology (actually, just the spelling) of NUL vs NULL. In my opinion, as long as you know that you are talking about putting a 0 somewhere, it doesn't matter whether you call it a "nul-terminator" or a "null-terminator". If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                J Offline
                                J Offline
                                James R Twine
                                wrote on last edited by
                                #15

                                Personally, I did not vote you down.  Since you have no idea that I did or not, I have to guess that the mentioning my NUL vs. NULL point is but a coincidence - otherwise it would be a highly inappropriate assumption on your part.  Also, you will note that even if I did vote you down, there are 4 other votes there as well.  Did it ever occur to you that perhaps others know something that you do not?  (And that just because you do not know what someone else is talking about, that does not mean that they do not?)    For the record, I voted your two earlier posts a 4.  The fact that the first is now below that value may be indicative of something else.    NUL is one thing, NULL (in uppercase) is another.  They are not two ways to spell the same thing, even though they have the same value.  NUL is the name/mnemonic for an ASCII encoded character with a value of zero.  BS, ACK, and NAK are similar characters.  I am sure you (should?) know the history of NULL, even in C.    Consider this - my calling "C++" something else, like "G--; that C-ish language with the objects, and references, and virtual thingys" is not correct, even if I know that I am really talking about.    Peace! -=- James


                                If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                                Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                                DeleteFXPFiles & CheckFavorites (Please rate this post!)

                                N Z 2 Replies Last reply
                                0
                                • N Nish Nishant

                                  Why is Zac's answer low-voted? It was an accurate answer, wasn't it? :confused: Regards, Nish


                                  Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                                  Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

                                  J Offline
                                  J Offline
                                  James R Twine
                                  wrote on last edited by
                                  #16

                                  It was mostly accurate, Nish... :P    Peace! -=- James


                                  If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                                  Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                                  DeleteFXPFiles & CheckFavorites (Please rate this post!)

                                  1 Reply Last reply
                                  0
                                  • J James R Twine

                                    Personally, I did not vote you down.  Since you have no idea that I did or not, I have to guess that the mentioning my NUL vs. NULL point is but a coincidence - otherwise it would be a highly inappropriate assumption on your part.  Also, you will note that even if I did vote you down, there are 4 other votes there as well.  Did it ever occur to you that perhaps others know something that you do not?  (And that just because you do not know what someone else is talking about, that does not mean that they do not?)    For the record, I voted your two earlier posts a 4.  The fact that the first is now below that value may be indicative of something else.    NUL is one thing, NULL (in uppercase) is another.  They are not two ways to spell the same thing, even though they have the same value.  NUL is the name/mnemonic for an ASCII encoded character with a value of zero.  BS, ACK, and NAK are similar characters.  I am sure you (should?) know the history of NULL, even in C.    Consider this - my calling "C++" something else, like "G--; that C-ish language with the objects, and references, and virtual thingys" is not correct, even if I know that I am really talking about.    Peace! -=- James


                                    If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                                    Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                                    DeleteFXPFiles & CheckFavorites (Please rate this post!)

                                    N Offline
                                    N Offline
                                    Nish Nishant
                                    wrote on last edited by
                                    #17

                                    Raymond Chen says null-terminated string here :- http://blogs.msdn.com/oldnewthing/archive/2004/08/24/219444.aspx[^] Most of MSDN documentation says null-terminated string too. Maybe that's why people use that (null I mean) more often. Regards, Nish


                                    Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                                    Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

                                    J 1 Reply Last reply
                                    0
                                    • J James R Twine

                                      Personally, I did not vote you down.  Since you have no idea that I did or not, I have to guess that the mentioning my NUL vs. NULL point is but a coincidence - otherwise it would be a highly inappropriate assumption on your part.  Also, you will note that even if I did vote you down, there are 4 other votes there as well.  Did it ever occur to you that perhaps others know something that you do not?  (And that just because you do not know what someone else is talking about, that does not mean that they do not?)    For the record, I voted your two earlier posts a 4.  The fact that the first is now below that value may be indicative of something else.    NUL is one thing, NULL (in uppercase) is another.  They are not two ways to spell the same thing, even though they have the same value.  NUL is the name/mnemonic for an ASCII encoded character with a value of zero.  BS, ACK, and NAK are similar characters.  I am sure you (should?) know the history of NULL, even in C.    Consider this - my calling "C++" something else, like "G--; that C-ish language with the objects, and references, and virtual thingys" is not correct, even if I know that I am really talking about.    Peace! -=- James


                                      If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                                      Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                                      DeleteFXPFiles & CheckFavorites (Please rate this post!)

                                      Z Offline
                                      Z Offline
                                      Zac Howland
                                      wrote on last edited by
                                      #18

                                      I wasn't referring to you personally. I actually hadn't even read your response yet when I posted that. On similar topics in the past (not necessarily on this forum, but others), I've been flamed for the nul vs null argument. My point is that as long as the reader understands what you are saying, it doesn't matter how you spell it. And yes, I'm well aware of the history of ASCII characters ... use to have to use them all the time in my last job (ACK/NAK especially). If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                      1 Reply Last reply
                                      0
                                      • N Nish Nishant

                                        Raymond Chen says null-terminated string here :- http://blogs.msdn.com/oldnewthing/archive/2004/08/24/219444.aspx[^] Most of MSDN documentation says null-terminated string too. Maybe that's why people use that (null I mean) more often. Regards, Nish


                                        Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                                        Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

                                        J Offline
                                        J Offline
                                        James R Twine
                                        wrote on last edited by
                                        #19

                                        Yep - lots of people also do not come to a complete stop at a stop sign, and still manage to get someplace in one piece. :) And you should know by now that you should never use or site Microsoft as a model for anything software-related! :P :P :P    Peace! -=- James


                                        If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                                        Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                                        DeleteFXPFiles & CheckFavorites (Please rate this post!)

                                        1 Reply Last reply
                                        0
                                        • J James R Twine

                                          The standard string-handling functions like strcpy(...), strcat(...), sprintf(...), etc. will write a terminating NUL (not a NULL[^]) into the buffer you are using.    So if you use them, you normally do not have to worry about previous data causing issues (unless you are concerned about security, but that is another issue).  Here is an example:

                                          char caBuffer[ 50 ];

                                          strcpy( caBuffer, "A Looooooooong String" );
                                          strcpy( caBuffer, "A Short String" );
                                          puts( caBuffer );

                                          After the first call to strcpy(..) the buffer will contain (NUL is represented by Ø):

                                          0123456789012345678901
                                          

                                          -and after the second call to strcpy(...), the buffer will likely contain:

                                          0123456789012345678901
                                          

                                          When the string is shown using puts(...), it will display A Short String, even though the actual memory for the buffer has the extra data in it.  Since string-handling functions use NUL as an end-of-string indicator, puts(...) stops when it gets to the first NUL it encounters.    Peace! -=- James


                                          If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                                          Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                                          DeleteFXPFiles & CheckFavorites (Please rate this post!)

                                          J Offline
                                          J Offline
                                          jhwurmbach
                                          wrote on last edited by
                                          #20

                                          James R. Twine wrote:

                                          write a terminating NUL (not a NULL[^])

                                          You really got a obsession there, right? :-D


                                          "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.

                                          J 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