locating memory- easy(char[])
-
if you declare the amount of info that you want to store in a char like... char ID[10]; that would mean that id has 10 slots + \0; right? now, if i would want to print out all the info from char ID... wouldnt i just.. cout << ID;? and if you wrote: cout << ID[2]; you would get the third memory slot, right? why cant i set M_ID = ID? M_ID is [10] and so is ID. bool ID::SetID(char ID[10]) { M_ID = ID return true; } i get an error when i put this in, and the only way i get rid of it is if i make: ID[]; and within the [] i would have to have a number. but i want all of ID = M_ID.., what do i do? what do i put in the []? also... if there is a good tutorial that teaches how to compare data, and all the little detail of do what im trying to do, please make note of it. Thanks! ~SilverShalkin :rose: ps... I started this message like 1-2 hours ago, and kept jumping back to my code and trying new things... so if the message is unclear about my mainpoint "question" just tell me, and i will refrase it in a more understandable way :)
-
A C-style string is stored in a char array, however a char array is not magically a string. You must ensure that the array is properly null-terminated. The way you do that is to always use the string functions when manipulating strings. SilverShalkin wrote: char ID[10]; that would mean that id has 10 slots + \0; right? No, that declares a 10-character array. Again, it's not magically a string. It just contains 10 characters. SilverShalkin wrote: now, if i would want to print out all the info from char ID... wouldnt i just.. cout << ID;? As long as ID is null-terminted, that's correct. SilverShalkin wrote: bool ID::SetID(char ID[10]) { M_ID = ID; } That is an error because you can't assign strings (or any arrays) with =. You copy a string with
strcpy()
. --Mike-- Just released - RightClick-Encrypt - Adds fast & easy file encryption to Explorer Like the Google toolbar? Then check out UltraBar, with more features & customizable search engines! My really out-of-date homepage Sonork - 100.10414 AcidHelmthanks! i was using strcpy() but then switched over because things werent working... ill try it again. Any tutorials? Thanks again! ~SilverShalkin :rose:
-
if you declare the amount of info that you want to store in a char like... char ID[10]; that would mean that id has 10 slots + \0; right? now, if i would want to print out all the info from char ID... wouldnt i just.. cout << ID;? and if you wrote: cout << ID[2]; you would get the third memory slot, right? why cant i set M_ID = ID? M_ID is [10] and so is ID. bool ID::SetID(char ID[10]) { M_ID = ID return true; } i get an error when i put this in, and the only way i get rid of it is if i make: ID[]; and within the [] i would have to have a number. but i want all of ID = M_ID.., what do i do? what do i put in the []? also... if there is a good tutorial that teaches how to compare data, and all the little detail of do what im trying to do, please make note of it. Thanks! ~SilverShalkin :rose: ps... I started this message like 1-2 hours ago, and kept jumping back to my code and trying new things... so if the message is unclear about my mainpoint "question" just tell me, and i will refrase it in a more understandable way :)
A C-style string is stored in a char array, however a char array is not magically a string. You must ensure that the array is properly null-terminated. The way you do that is to always use the string functions when manipulating strings. SilverShalkin wrote: char ID[10]; that would mean that id has 10 slots + \0; right? No, that declares a 10-character array. Again, it's not magically a string. It just contains 10 characters. SilverShalkin wrote: now, if i would want to print out all the info from char ID... wouldnt i just.. cout << ID;? As long as ID is null-terminted, that's correct. SilverShalkin wrote: bool ID::SetID(char ID[10]) { M_ID = ID; } That is an error because you can't assign strings (or any arrays) with =. You copy a string with
strcpy()
. --Mike-- Just released - RightClick-Encrypt - Adds fast & easy file encryption to Explorer Like the Google toolbar? Then check out UltraBar, with more features & customizable search engines! My really out-of-date homepage Sonork - 100.10414 AcidHelm -
thanks! i was using strcpy() but then switched over because things werent working... ill try it again. Any tutorials? Thanks again! ~SilverShalkin :rose:
SilverShalkin wrote: Any tutorials? Any C beginners book. And you can look up in MSDN if you have any problems. Best regards, Alexandru Savescu