Memory compareing with char's
-
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
-
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
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.
-
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.
ill try it out... thanks! ~SilverShalkin :rose:
-
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.
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:
-
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:
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.
-
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.
Oops! The #includes didn't go through. #include <istream.h> #include <string.h> Like it or not, I'm right.
-
Oops! The #includes didn't go through. #include <istream.h> #include <string.h> Like it or not, I'm right.
<istream.h>
is deprecated. Instead, you should be using<istream>
, possibly followed byusing namespace std;
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
<istream.h>
is deprecated. Instead, you should be using<istream>
, possibly followed byusing namespace std;
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
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:
-
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:
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