Monday, May 24, 2010

Help me(C++ qustion)?

what is the wrong with my programme ???!!!


it doesn`t run!!


can any one help me why that??


#include %26lt;iostream%26gt;


using namespace std;





void CopyString (char *dest, char *src){





cout%26lt;%26lt;"\n Call CopyString function .... ";


cout%26lt;%26lt;"\n before copy : Source string is%26gt;%26gt; "%26lt;%26lt;src%26lt;%26lt;" and destination is%26gt;%26gt; "%26lt;%26lt;dest;


while (*dest++ = *src++);


cout%26lt;%26lt;"\n after copy : Source string is%26gt;%26gt; "%26lt;%26lt;src%26lt;%26lt;" and destination is%26gt;%26gt; "%26lt;%26lt;dest;


cout%26lt;%26lt;"\n ========================================...


}





int main(){





char *sword = "SWords", *dword = "DWords";


char sarray[10] = "Sarray", darray[10] = "Darray";


char *sptr=sword , *dptr;





dptr = dword;





CopyString( "This is the destination", "This is the Source" );


CopyString(sword,dword);


CopyString(sarray,darray);


CopyString(sptr,dptr);


sptr = sarray; dptr = darray;


CopyString(sptr,dptr);





return 0;}

Help me(C++ qustion)?
There are several benign bugs in this program that would manifest themselves if you were to try to use CopyString in a useful way, but the 1 bug that will cause this not to even compile is your last cout. It does not have an end quote or a semicolon after it.
Reply:I wish I could point out an easy fix for you, but there are a few problems that need work. Arrays are a b*tch when you are first starting out.





When working with character arrays, you need to know their length. You can't just ++ through them. You won't know when to stop and will get an access violation exception. One way to know when to stop is check each character to see if it is null (if c == '\0'). That's the end of the string. Copy it and get outta there.





Second major issue is that your destination string is an array with a particular size. If the source string is longer than that dest array, you're outta luck. You need to figure out how you are going to deal with array size. Will you have a maximum size? Or should CopyString use new to allocate a string on the heap and return that ponter?





Let's keep it simple and say there is a max length of 1024.


There's no reason to initialize the destination array to some string value. Just create the array of a particular length, maybe initializing it to '\0' if you want to cout it before you are done with CopyString. There is no problem if an array is longer than the string (the null terminator defines the end of the string). There is a big problem if the array is shorter than the string, or if any code tries to access the array past its bounds.





One more thing. using ++ on dest and src is actually redefining the pointer. By the time you are done, dest and src will be pointing to the end of the string (once you fix the length problem, else you'll just access violate). Since you want to cout src and dest, you want to retain the original pointer position at the beginning of the string. Use [ ] to access particular elements of the array instead of using pointer arithmatic ++.





Something like this...





void CopyString (char *dest, char *src){


cout%26lt;%26lt;"\n Call CopyString function .... ";


for (int i = 0 ; src[i] != '\0' ; i++)


{


dest[i] = src[i];


}


dest[i] = '\0'; //put the terminating null on the dest string.





cout%26lt;%26lt;"\n after copy : Source string is%26gt;%26gt; "%26lt;%26lt;src%26lt;%26lt;" and destination is%26gt;%26gt; "%26lt;%26lt;dest;


cout%26lt;%26lt;"\n ==============================";


}





int main(){





char* testSrc = "the String";


char testDest[1024];





CopyString(testDest, testSrc);





return 0;}

jasmine

No comments:

Post a Comment