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. Memory compareing with char's

Memory compareing with char's

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiondata-structuresperformance
9 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.
  • S Offline
    S Offline
    SilverShalkin
    wrote on last edited by
    #1

    ok... this shouldnt be a hard question to answer, but knowing my luck... :) i have a pointer in my header called: char *M_Name; The problem accures in this function: I want to Set the name that you enter in, then have that info stored in an array... in this case the array is char Names[21];// for 20 names. bool Name::SetName(char* NName) { if(!NName)//checking input { return false; } int NLen = (strlen(NName) + 1); char *NewName = new char[NLen]; if(!NewName)//checking if allocation faild or not { return false; } strcpy(NewName, NName); delete[] M_Name; M_Name = NewName; Names[NN] = *M_Name; NN++; return true; } when it gets to M_Name = NewName, it sets the name you entered into the pointer. Then i wanted to store that same name into the array "stating at 0" so i put in: Names[NN] = *M_Name; //then i increase NN by one to change the memory slot. the only problem is... when i try to print this info... i only get the first character of whatever name i insert. im not to sure on why... i think i need to make each of the data 0-19 have an amount of memory attached to them, is this the right code to insert: char Names[21][32];? and if that is the right code to insert, how would i set M_Name to = to 32 bytes of memory without declaring: char *M_Name[32]; ? Thanks for the help! ~SilverShalkin

    J 1 Reply Last reply
    0
    • S SilverShalkin

      ok... this shouldnt be a hard question to answer, but knowing my luck... :) i have a pointer in my header called: char *M_Name; The problem accures in this function: I want to Set the name that you enter in, then have that info stored in an array... in this case the array is char Names[21];// for 20 names. bool Name::SetName(char* NName) { if(!NName)//checking input { return false; } int NLen = (strlen(NName) + 1); char *NewName = new char[NLen]; if(!NewName)//checking if allocation faild or not { return false; } strcpy(NewName, NName); delete[] M_Name; M_Name = NewName; Names[NN] = *M_Name; NN++; return true; } when it gets to M_Name = NewName, it sets the name you entered into the pointer. Then i wanted to store that same name into the array "stating at 0" so i put in: Names[NN] = *M_Name; //then i increase NN by one to change the memory slot. the only problem is... when i try to print this info... i only get the first character of whatever name i insert. im not to sure on why... i think i need to make each of the data 0-19 have an amount of memory attached to them, is this the right code to insert: char Names[21][32];? and if that is the right code to insert, how would i set M_Name to = to 32 bytes of memory without declaring: char *M_Name[32]; ? Thanks for the help! ~SilverShalkin

      J Offline
      J Offline
      Jason Henderson
      wrote on last edited by
      #2

      SilverShalkin wrote: char Names[21];// for 20 names. Names is not 20 strings, its 20 characters. Yes, you could use char Names[21][32]. SilverShalkin wrote: and if that is the right code to insert, how would i set M_Name to = to 32 bytes of memory without declaring: char *M_Name[32]; Try this: char Names[21][32]; bool Name::SetName(char* NName) { if(!NName)//checking input { return false; } strncpy(Names[NN], NName, 32); NN++; return true; } Like it or not, I'm right.

      S 2 Replies Last reply
      0
      • J Jason Henderson

        SilverShalkin wrote: char Names[21];// for 20 names. Names is not 20 strings, its 20 characters. Yes, you could use char Names[21][32]. SilverShalkin wrote: and if that is the right code to insert, how would i set M_Name to = to 32 bytes of memory without declaring: char *M_Name[32]; Try this: char Names[21][32]; bool Name::SetName(char* NName) { if(!NName)//checking input { return false; } strncpy(Names[NN], NName, 32); NN++; return true; } Like it or not, I'm right.

        S Offline
        S Offline
        SilverShalkin
        wrote on last edited by
        #3

        ill try it out... thanks! ~SilverShalkin :rose:

        1 Reply Last reply
        0
        • J Jason Henderson

          SilverShalkin wrote: char Names[21];// for 20 names. Names is not 20 strings, its 20 characters. Yes, you could use char Names[21][32]. SilverShalkin wrote: and if that is the right code to insert, how would i set M_Name to = to 32 bytes of memory without declaring: char *M_Name[32]; Try this: char Names[21][32]; bool Name::SetName(char* NName) { if(!NName)//checking input { return false; } strncpy(Names[NN], NName, 32); NN++; return true; } Like it or not, I'm right.

          S Offline
          S Offline
          SilverShalkin
          wrote on last edited by
          #4

          Jason Henderson wrote: strncpy(Names[NN], NName, 32); i put this in... but the , 32) doesnt work, but now the command works! so im not sure if i needed the 32.. hmmm :) Thanks for the help! ~SilverShalkin :rose:

          J 1 Reply Last reply
          0
          • S SilverShalkin

            Jason Henderson wrote: strncpy(Names[NN], NName, 32); i put this in... but the , 32) doesnt work, but now the command works! so im not sure if i needed the 32.. hmmm :) Thanks for the help! ~SilverShalkin :rose:

            J Offline
            J Offline
            Jason Henderson
            wrote on last edited by
            #5

            The 32 in strncpy tells the function the number of chars to copy the the string. You don't want it to copy past the bounds of your array. If you send the function a 40 char string like "0123456789012345678901234567890123456789", it should cut it off at 32. Here's a little console app to test it:

            #include #include char Names[21][32];
            int NN = 0;

            void vSetName(char* NName)
            {
            strncpy(Names[NN], NName, 32);
            NN++;
            }

            void main()
            {
            char inp;
            vSetName("testing");
            vSetName("0123456789012345678901234567890123456789");
            cout << Names[0];
            cout << "\r\n";
            cout << Names[1];
            cin >> inp;
            }

            Like it or not, I'm right.

            J 1 Reply Last reply
            0
            • J Jason Henderson

              The 32 in strncpy tells the function the number of chars to copy the the string. You don't want it to copy past the bounds of your array. If you send the function a 40 char string like "0123456789012345678901234567890123456789", it should cut it off at 32. Here's a little console app to test it:

              #include #include char Names[21][32];
              int NN = 0;

              void vSetName(char* NName)
              {
              strncpy(Names[NN], NName, 32);
              NN++;
              }

              void main()
              {
              char inp;
              vSetName("testing");
              vSetName("0123456789012345678901234567890123456789");
              cout << Names[0];
              cout << "\r\n";
              cout << Names[1];
              cin >> inp;
              }

              Like it or not, I'm right.

              J Offline
              J Offline
              Jason Henderson
              wrote on last edited by
              #6

              Oops! The #includes didn't go through. #include <istream.h> #include <string.h> Like it or not, I'm right.

              J 1 Reply Last reply
              0
              • J Jason Henderson

                Oops! The #includes didn't go through. #include <istream.h> #include <string.h> Like it or not, I'm right.

                J Offline
                J Offline
                Joaquin M Lopez Munoz
                wrote on last edited by
                #7

                <istream.h> is deprecated. Instead, you should be using <istream>, possibly followed by

                using namespace std;

                Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                S 1 Reply Last reply
                0
                • J Joaquin M Lopez Munoz

                  <istream.h> is deprecated. Instead, you should be using <istream>, possibly followed by

                  using namespace std;

                  Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                  S Offline
                  S Offline
                  SilverShalkin
                  wrote on last edited by
                  #8

                  did you know joaquin is the name of my streat? your famous!!!! Thanks for the help again :) and again, and again :) its always appreciated ~SilverShalkin :rose:

                  J 1 Reply Last reply
                  0
                  • S SilverShalkin

                    did you know joaquin is the name of my streat? your famous!!!! Thanks for the help again :) and again, and again :) its always appreciated ~SilverShalkin :rose:

                    J Offline
                    J Offline
                    Joaquin M Lopez Munoz
                    wrote on last edited by
                    #9

                    did you know joaquin is the name of my streat? your famous!!!! Are you by chance from California? There's a lot of Spanish toponyms there. By the way, in Madrid (where I live) there's a street with my exact name "Joaquín María López" --it was some politician from the XIX century. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                    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